Java's L Number (Long) Specification

Java's L number (long) specification

There are specific suffixes for long (e.g. 39832L), float (e.g. 2.4f) and double (e.g. -7.832d).

If there is no suffix, and it is an integral type (e.g. 5623), it is assumed to be an int. If it is not an integral type (e.g. 3.14159), it is assumed to be a double.

In all other cases (byte, short, char), you need the cast as there is no specific suffix.

The Java spec allows both upper and lower case suffixes, but the upper case version for longs is preferred, as the upper case L is less easy to confuse with a numeral 1 than the lower case l.

See the JLS section 3.10 for the gory details (see the definition of IntegerTypeSuffix).

Can somebody explain to me why the L (long) has to be there?

Because the literal value will always be interpreted as int, if not appended with l or L.

You would be assigning a literal int to a variable of type long if the l wasn't specified.

At compile-time, the compiler checks on the literal value first and if l or L is not appended, it will interpret it as int.

Now, if the number is larger than Integer.MAX_VALUE, the compiler will display an error.

Explicit specification of an integral type, Long

This is perfectly intended.

Think about how the program is parsed. The lexer which splits the Java source into tokens knows about two things of literal:

  • the ones which don't end with [L|l]
  • the ones which end with [L|l]

A literal which doesn't have the long specifier at the end is parsed as an integer. This means that the literal itself is invalid because you are not allowed to define an integer literal which is larger than the larger number that you can represent with an int.

This is clear in the grammar specification here:

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

It is a compile-time error if a decimal literal of type int is larger than 2147483648 (231), or if the decimal literal 2147483648 appears anywhere other than as the operand of the unary minus operator (§15.15.4).

So even without any assignment, if you have the expression

999_999_999_999

This is parsed as an integer literal, but its value is over the maximum allowed value, hence it is illegal. The fact that the value of this expression is later assigned to something is irrelevant.

How to tell java that this number could become long during runtime?

you need use BigInteger , it take the memory dinamicaly so he dont have limit.

Java LONG integer types

Long myUserId = 1;   // error

does not work, because 1 is an int.

It will get auto-boxed to:

Integer myUserId = 1;   // ok

It will also get widened to:

long myUserId = 1;      // also ok

but not both.

So, yes, you have to say

Long myUserId = 1L;  

which is a long that can get autoboxed into a Long.

As to why it works that way (or rather does not work in this case): Most likely because auto-boxing was added later (in Java5), and had to be absolutely backwards-compatible. That limited how "smooth" they could make it.



Related Topics



Leave a reply



Submit