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.
Since the parsing of literals happens at compile time, this is absolutely irrelevant in regard to performance. The only reason having
short
andbyte
suffixes would be nice is that it lead to more compact code.Consider:
versus
Now you'd expect bother code fragments to give the same value to variable
l
. So we need expression onint
literals to be done asint
s.These are literals and are described in section 3.10 of the Java language spec.
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:
to overcome this perform type casting.
In case of long data type, it can accept the integer value without any hassle. Suppose we assign like
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.
Here we need to put suffix as L to treat the literal 2147483648 as long type by java compiler.
so finally
There are specific suffixes for
long
(e.g.39832L
),float
(e.g.2.4f
) anddouble
(e.g.-7.832d
).If there is no suffix, and it is an integral type (e.g.
5623
), it is assumed to be anint
. If it is not an integral type (e.g.3.14159
), it is assumed to be adouble
.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
long
s is preferred, as the upper caseL
is less easy to confuse with a numeral1
than the lower casel
.See the JLS section 3.10 for the gory details (see the definition of
IntegerTypeSuffix
).I hope you won't mind a slight tangent, but thought you may be interested to know that besides
F
(for float),D
(for double), andL
(for long), a proposal has been made to add suffixes forbyte
andshort
—Y
andS
respectively. This would eliminate to the need to cast to bytes when using literal syntax for byte (or short) arrays. Quoting the example from the proposal:Joe Darcy is overseeing Project Coin for Java 7, and his blog has been an easy way to track these proposals.