It would be possible for a language to require that all integer arithmetic which would not overflow if intermediate results were computed with the longest integral type, as though they were. In such a language, if variables starting with L are 64 bits, those with W are 32-bits, and those with H are 16 bits, expressions like
L1 = W1*W2;
W3 = (W1+W2) >> 1
will be evaluated in such fashion as to avoid overflow, but an expression like
W4 = W1+W2
will be evaluated using 32-bit math (since any overflow that would occur then would occur with the assignment to W4 even if the intermediate result were evaluated as 32 bits), and an expression like
W5 = (H1*H2) >> 1
could be evaluated as 32 bits since the result couldn't overflow a 32-bit value.
Such a language could be quite efficient in most scenarios, since it would generally not hard for a compiler to determine the maximum relevant integer size for each subexpression. In such a language, it wouldn't matter whether a numeric literal was a "long" or an "int", since the compiler would be more interested in its numeric value.
In Java, however, numeric literals of different sizes have different semantics. If one multiplies an int
by the constant 128, the int
must be no larger than 16,777,215 or else an overflow will occur. If one multiplies the int
by a constant 128L
, the result may only be stored someplace that can accept a long
.