I've read that to declare a long, you need to suffix the number with l
or L
So far, so good. But what happens, if I omit this suffix?
long l1 = 100l; // ok
long l2 = 100L; // ok
long l3 = 100; // ?
Is l3
now an integer or still a long?
I've read that to declare a long, you need to suffix the number with l
or L
So far, so good. But what happens, if I omit this suffix?
long l1 = 100l; // ok
long l2 = 100L; // ok
long l3 = 100; // ?
Is l3
now an integer or still a long?
long l3 = 100
declares a long, assigns an int
to it (100
is an int literal), which results in the int being promoted to long implicitly.
The int
range, however, has limits. There are valid long
values that can't be declared without the L
suffix:
long l3 = 2147483648; //doesn't compile
2147483647
is the max value for int and anything above it can't be used without the l
suffix as a long literal. In fact, using just 2147483648
anywhere in Java source code won't compile because without any suffix, the literal is expected to be a valid int, and that number is too big for an int.