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

2

37

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?

Wow answered 3/12, 2016 at 23:8 Comment(0)
H
43

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

Hearne answered 3/12, 2016 at 23:10 Comment(2)
Don't longs give null pointer exceptions more frequently than Longs though?Caricature
So, long story short primarily that difference l vs L is kept just for keeping it more readable. (And to prevent confusion from digit 1)Retention
R
24

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.

Reside answered 3/12, 2016 at 23:11 Comment(2)
Then why do they have lowercase long as an object type?Caricature
@CollinFox long is a primitive 64-bit data type in Java. Long is an object type.Hearne

© 2022 - 2024 — McMap. All rights reserved.