My question is long a = 1; an integer in reality? And is it the same as long a = 1L; in data type?
>Solution :
You can see from the following experiments that the literals (not prefixed with L) are ints, but that there are automatic casts such that a long variable finaly always holds a long value:
long a = 2147483647 + 1;
long b = 2147483647;
b++;
long c = 2147483647L + 1;
- For
a, the literals areints. The calclulation is done withints, which overflows. Thereforelong agets assigned-2147483648L. - For
b, theintliteral is casted tolongduring assignment, which can be incremented to the final valueb = 2147483648Lwithout overflow. - For
c, the calculation is already done withlongs (theintliteral1is automatically casted to1L), givingc = 2147483648Lin first place.
You can find the specification for numeric promotion in JLS 5.6 "Numeric Contexts"