We provide some additional information here about integers, as
well as stressing again some points made previously. This continues
the discussion of integers in Chapter
2: Java : Primitive data types
Java offers 5 types of integer:
- byte
- 8-bit, signed
- short
- 16-bit, signed
- int
- 32-bit, signed
- long
- 64-bit, signed
- char
- 16-bit, can be treated as unsigned integer in some situations
The table
showed the ranges of values of each type. The pages Chapter
2: Java : Casting & Mixing and Chapter
2: Tech : More Casting & Mixing discuss how the types can
be converted from one to another.
Here are some other aspects of using integers:
- No overflow and underflow warnings
- unlike the case for the floating point types, there are no special
values for the integer types that indicate that the maximum
value has been exceeded or that a value has gone below the negative
minimum values. Exceeding the maximum value will flip the value
into the negative range while continuing past the minimum negative
value will flip the value into the positive range.
- Maximum and minimum values constants
- the wrapper
classes (to be discussed later)
provide constants that give the maximum and minimum values for
each type. These can can be useful for testing for overflows/underflows
since, as discussed above, no exceptions are thrown in such cases.
- byte
- Byte.MIN_VALUE
-27
= -128
- Byte.MAX_VALUE
27-1
= 127
- short
- short.MIN_VALUE
-215
= -32768
- Short.MAX_VALUE
215-1
= 32767
- int
- Integer.MIN_VALUE
-231
= -2147483648
- Integer.MAX_VALUE
231-1
= 2147483647
- long
- Long.MIN_VALUE
-263
= -9223372036854775808l
- Long.MAX_VALUE
263-1
= 9223372036854775807l
- Divide by zero errors -
unlike the floating point types ( float
and double),
integer divide by zero leads to a run time exception
that will stop the processing unless caught as discussed later.
References & Web Resources
Latest update: Oct. 15, 2004
|