Java Reference
In-Depth Information
Each of the integer types has a different rangeȌAdvanced Topic 4.2 explains why
the range limits are related to powers of two. Generally, you will use the int type for
integer quantities. However, occasionally, calculations involving integers can
overflow. This happens if the result of a computation exceeds the range for the
number type. For example:
A numeric computation overflows if the result falls outside the range for the
number type.
int n = 1000000;
System.out.println(n * n); // Prints-727379968
The product n * n is 10 12 , which is larger than the largest integer (about 2 – 10 9 ).
The result is truncated to fit into an int , yielding a value that is completely wrong.
Unfortunately, there is no warning when an integer overflow occurs.
134
135
Table 1 Primitive Types
Type
Description
Size
int
The integer type, with range ɰ2,147,483,648 Ȟ
2,147,483,647 (about 2 billion)
4 bytes
byte
The type describing a single byte, with range ɰ128 Ȟ
127
1 byte
short
The short integer type, with range ɰ32768 Ȟ 32767
2 bytes
long
The long integer type, with range
ɰ9,223,372,036,854,775,808 Ȟ
9,223,372,036,854,775,807
8 bytes
double
The double-precision floating-point type, with a range
of about 10 308 and about 15 significant decimal digits
8 bytes
float
The single-precision floating-point type, with a range of
about 10 38 and about 7 significant decimal digits
4 bytes
char
The character type, representing code units in the
Unicode encoding scheme (see Advanced Topic 4.5 )
2 bytes
boolean
The type with the two truth values false and true (see
Chapter 5 )
1 bit
 
Search WWH ::




Custom Search