Integer Data Type: no fractional part
What is the range of the data type
int? (-2^31 to
2^31 -1) (-2147483648 to 2147483647)
It is 4 bytes. How do you get the range?
1 byte = 8 bits. 4 bytes = 32 bits. Leave one bit for + or -
sign.
A. Try the following program when initialize a out of range variable:
public class Range
{
public static void main(String [] args)
{
int n = -2147483649; //-2^31 - 1
System.out.println(n);
}
}
Note: When you compile it, the compiler has informed you the error message as bellowed.
B. However, for the overflow under a calculation, CPU will not inform user as bellowed.
public class Range
{
public static void main(String [] args)
{
int n = 65536; //2^16
System.out.println(n
* n); //2^32
}
}
Given a wrong result if calculation is overflow.