1

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?

mrbela
  • 4,477
  • 9
  • 44
  • 79
  • 2
    `long l3 = 100;`<- `100` is an integer, `l3` is a long. You can assign an integer to a long an java will do the conversation for you. – OH GOD SPIDERS Mar 08 '21 at 11:49
  • https://stackoverflow.com/questions/17738232/using-the-letter-l-in-long-variable-declaration – M A Mar 08 '21 at 11:52

1 Answers1

7

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.

ernest_k
  • 44,416
  • 5
  • 53
  • 99
  • 1
    Mandatory JLS reference: integer literals ([JLS § 3.10.1](https://docs.oracle.com/javase/specs/jls/se15/html/jls-3.html#jls-3.10.1)); primitive types in assigmnent contexts are subject to widening ([JLS § 5](https://docs.oracle.com/javase/specs/jls/se15/html/jls-5.html)). – MC Emperor Mar 08 '21 at 12:28