Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java's L number (long) specification

It appears that when you type in a number in Java, the compiler automatically reads it as an integer, which is why when you type in (long) 6000000000 (not in integer's range) it will complain that 6000000000 is not an integer. To correct this, I had to specify 6000000000L. I just learned about this specification.

Are there other number specifications like for short, byte, float, double? It seems like these would be good to have because (I assume) if you could specify the number you're typing in is a short then java wouldn't have to cast it - that is an assumption, correct me if I'm wrong. I would normally search this question myself, but I don't know what this kind of number specification is even called.

like image 518
jbu Avatar asked Apr 20 '09 20:04

jbu


People also ask

What is 1L long in Java?

(long) 1 is a constant expression (because the 1 is directly known) and hence, by the rules of the Java Language Specification (JLS), will be a long after compilation already. However, in my experience, it is far more common that people use the long literal and write 1L for readability.

Why we put L for long in Java?

The L suffix tells the compiler that we have a long number literal. Java byte , short , int and long types are used do represent fixed precision numbers. This means that they can represent a limited amount of integers. The largest integer number that a long type can represent is 9223372036854775807.

How do you specify long?

To specify a numeric literal as a long instead of an int , add an L (for long ) to the end of the literal. Either capital or lowercase will work, but a lowercase 'l' can easily be confused with the numeral '1', especially in monospace fonts.


2 Answers

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

like image 86
Simon Nickerson Avatar answered Sep 19 '22 17:09

Simon Nickerson


By default any integral primitive data type (byte, short, int, long) will be treated as int type by java compiler. For byte and short, as long as value assigned to them is in their range, there is no problem and no suffix required. If value assigned to byte and short exceeds their range, explicit type casting is required.

Ex:

byte b = 130; // CE: range is exceeding. 

to overcome this perform type casting.

byte b = (byte)130; //valid, but chances of losing data is there. 

In case of long data type, it can accept the integer value without any hassle. Suppose we assign like

long l = 2147483647; //which is max value of int 

in this case no suffix like L/l is required. By default value 2147483647 is considered by java compiler is int type. Internal type casting is done by compiler and int is auto promoted to Long type.

long l = 2147483648; //CE: value is treated as int but out of range  

Here we need to put suffix as L to treat the literal 2147483648 as long type by java compiler.

so finally

long l = 2147483648L;// works fine. 
like image 25
Utpal Kumar Avatar answered Sep 20 '22 17:09

Utpal Kumar