Geddi Geddi - 1 year ago 62
Java Question

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

Let's say i declare this variable:

long k = 1060606060000;

If i do that I get an error, because the number is obviously to large for an integer. Why do I have to add the L at the end for the compiler to recognise that it is a long variable? Even though I obviously said I need k to be of the type long.

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download