LargeIntegerLong

Misconception:

A number like 4000000000000, which is too big to fit into a 32-bit signed int, is interpreted as a literal of type long.

Incorrect

Large integer numbers have type long

Correct

Integer numbers without L or l suffix that are too large for an int are illegal

Correction
Here is what's right.

In Java, large integral literals do not implicitly have type long. To get a long literal, one needs to add an L or l as a suffix.

The following code would not compile:

long number = 12345678901234; // ERROR: integer number too large

The following code also would not compile:

long number = (long)12345678901234; // ERROR: integer number too large

The following tree shows the reason for this compiler error: (long)12345678901234 is an expression consisting of a cast operator (long) and its operand, the number 12345678901234. However, as shown above, that operand is not a legal literal, and thus the expression will not compile.

Java provides the suffix L or l to allow expressing a long value as a literal:

long number = 12345678901234L; // OK: long literal

As the following tree shows, the literal 12345678901234L is an expression of type long with value 12345678901234.

Stay up-to-date

Follow us on  twitter to hear about new misconceptions.