Java Reference
In-Depth Information
The types are: the integral types ( byte , short , int , long , and char ), the
floating point types ( float and double ), and type boolean . A beginner needs to
look only at types int , double , and boolean .
Type int
The values of type int are the integers in the range:
-2147483648..+2147483647, or -2 31 ..2 31 -1 .
A value of type int occupies four bytes of memory.
Use the following constants to access the minimum and maximum values of
class int :
int literals
The conventional representation of integers, like 108 and 0 , are called liter-
als in Java. Such literals are expressions of type int , and they denote the obvi-
ous integer values. For example, adding 108 and 9 gives the decimal value 117 :
System.out.print(108 + 9) // Prints 117
Conventionally, in mathematics, a decimal integer can have leading zeros,
so 10 , 010 , and 0010 are equal. In Java, however, they are different! A leading
zero means that the octal, or base 8 , number system is being used, so that 10 and
010 represent different integers.
Designers of programming languages should not create notations that con-
flict with tradition, for they make for confusion and wasted time.
See a footnote
on lesson page
6-2 for the
octal number
system and for
octal and
int literals.
The int operations
The int operations are negation, unary addition, addition, subtraction, mul-
tiplication, division, and remainder. Given int operands, they produce an int .
We describe these operations assuming that E , E1 , and E2 are int expressions:
+E is unary addition . Its value is the value of E .
-E is conventional negation , or unary minus . Its value is the value of E
with its sign changed (from + to - or from - to + ).
Overflow! The largest int value is 2147483647 . The value of 2147483647+1 is not 2147483648
but -2147483648 ! When the result of an int operation is outside the range of
type int , overflow occurs, and the answer obtained is not what you expect. The
answer is determined by the fact that integers are represented in twos-comple-
ment notation, which is outside the scope of this topic. You will not be given a
warning when calculated values are outside the range of int and your answers
are not correct.
Search WWH ::

Custom Search