Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: Is there a difference between L and l (lowercase L) when specifying a long?

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?

like image 812
Lippstadtfreak Avatar asked Dec 03 '16 23:12

Lippstadtfreak


People also ask

How do you specify long in Java?

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.

What is difference between long and long in Java?

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.

What does 1L in Java mean?

(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.

Is 0L long?

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.


2 Answers

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 letter L or l (ell); otherwise it is of type int (§4.2.1).

The suffix L is preferred, because the letter l (ell) is often hard to distinguish from the digit 1 (one).

like image 145
Elliott Frisch Avatar answered Sep 24 '22 16:09

Elliott Frisch


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.

like image 27
duffymo Avatar answered Sep 22 '22 16:09

duffymo