I am working with data types at the moment in Java, and if I have understood correctly the type long
accepts a value between the ranges of -9,223,372,036,854,775,808 to +9,223,372,036,854,775,807. Now as you can see below, I have create a long
variable called testLong
, although when I insert 9223372036854775807 as the value, I get an error stating:
The literal 9223372036854775807 of the type int is out of range.
I don't know why it is referring to the long
data type as an int
.
Anyone have any ideas?
Code:
char testChar = 01;
byte testByte = -128;
int testInt = -2147483648;
short testShort = -32768;
long testLong = 9223372036854775807;
float testFoat;
double testDouble = 4.940656458412;
boolean testBool = true;
Add a capital L
to the end:
long value = 9223372036854775807L;
Otherwise, the compiler will try to parse the literal as an int
, hence the error message
I don't know why it is referring to the long data type as an int
It is not. You should learn to trust compiler messages (especially when they are from sane, modern compilers and not ancient compilers that tended to have bad error messages). While the language that they speak might be hard to decipher at times, they are not usually lying to you.
Let's look at it again:
The literal of int 9223372036854775807 is out of range.
Note, that it doesn't mention your variable testLong
or the type long
anywhere, so the problem is not about the initialization. It seems to occur at some other point.
Now lets investigate some of the parts of the message:
int
tells us that he wants to treat something as an int
value (which is not what you wanted!)int
)I'll leave the cozy list to talk about literals for a moment: literals are places where you have some value in your code. There are String
literals, int
literals, class
literals and so on. Every time you mention a value explicitly in your code, it's a literal.
So it's not actually nagging you about the variable declaration, but the number itself, the value is what it's nagging you about.
You can easily verify this by using the same literal in a context where a long
and an int
are equally acceptable:
System.out.println(9223372036854775807);
PrintStream.println
can take either an int
or a long
(or pretty much anything else). So that code should be fine, right?
No. Well, maybe it should be, but according to the rules it is not fine.
The problem is that "some digits" is defined to be an int
literal and therefore must be in the range defined by int
.
If you want to write a long
literal, then you must make that explicit by appending the L
(or lower case l
, but I highly suggest you always use the upper-case variant, because it's much easier to read and harder to mistake for a 1
).
Note that a similar problem occurs with float
(postfix F
/f
) and double
(postfix D
/d
).
Side note: you'll realize that there are no byte
or short
literals and you can still assign values (usually int
literals) to byte
and short
variables: that's possible due to special rules in § 5.2 about conversions in an Assignment Contexts: they allow assignment of constant expressions of a larger type to byte
, short
, char
or int
if the values are within the types range.
Try doing 9223372036854775807L
. The L
at the end tells Java that 9223372036854775807
is a long
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With