When I specify a number to be a long
with a constant value 400
, is there any difference between using 400L
and 400l
?
Does it have some relationship to the wrapper type? Is L
used to get a wrapper Long
and l
for the primitive data type long
?
To specify a numeric literal as a long instead of an int , add an L (for long ) to the end of the literal. Either capital or lowercase will work, but a lowercase 'l' can easily be confused with the numeral '1', especially in monospace fonts.
A Long is a class, or a reference type, defined in the standard library. It stores a reference to an object containing a value (a "box"). A long on the other hand, is a primitive type and part of the language itself.
(long) 1 is a constant expression (because the 1 is directly known) and hence, by the rules of the Java Language Specification (JLS), will be a long after compilation already. However, in my experience, it is far more common that people use the long literal and write 1L for readability.
The 0L means the number zero of type long . It uses this constructor to instantiate a Date that refers to zero milliseconds after (i.e. exactly) "the epoch", January 1, 1970, 00:00:00 GMT.
No practical difference. Either L
or l
can be used, both indicate a long
primitive. Also, either can be autoboxed to the corresponding Long
wrapper type.
However, it is worth noting that JLS-3.10.1 - Integer Literals says (in part)
An integer literal is of type
long
if it is suffixed with an ASCII letterL
orl
(ell); otherwise it is of typeint
(§4.2.1).The suffix
L
is preferred, because the letterl
(ell) is often hard to distinguish from the digit1
(one).
Yes: it's readability.
It's easy to mistake 400l
for four thousand and one when you first glance at it.
I find it more likely to interpret it correctly as four hundred long with the upper case L.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With