A sequence of digits is automatically an int value, and if it is too large to be
in the range of int , the program in which it appears will not compile. For exam-
ple, the following is syntactically incorrect even though the integer in it is in the
range of type long :
long x= 2147483648;
To make an int literal into a long literal, append L to it (either upper case
or lower case), with no whitespace before the L . The following initializing dec-
laration is syntactically correct:
long x= 2147483648L;
Operations of type long
The operations of type long are similar to those of type int . They are: nega-
tion, unary addition, addition, subtraction, multiplication, division, and remain-
der. Given long operands, they produce a long result. See Sec. 6.1 for a descrip-
tion of the operations and Sec. 6.3 for a discussion of casting.
Casting among integral types
Every byte value is in the range of short , every short value is in the range of
int , and every int value is in the range of long . We can depict this as follows:
byte short int long
We say that each type is narrower than the types to its right in this diagram
and wider than the types to its left. For example, short is narrower than long
and long is wider than short .
If an operand is supposed to be of a certain type but an expression of a nar-
rower type appears there, Java will promote it to the required type. For example,
suppose variable b is of type byte and the expression b+b is to be evaluated.
Types byte and short have no operations. The addition is an int addition. Each
operand is promoted from byte to int , the int addition is performed, and the
value of the expression has type int .
This, then, shows how byte and short values can be used even though no
operations —except conversions to other types— are defined on them.
Suppose byte variable b contains the value 9 . The assignment
b= (b + 1);
is syntactically illegal because an int value cannot be assigned to a byte vari-
able. But in this case, we know that the value of b+1 is in byte 's range, and we
would like to store that value in b .
In this situation, we precede the expression (b+1) by keyword byte
enclosed in parentheses: