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
Anyone have any ideas?
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:
inttells us that he wants to treat something as an
intvalue (which is not what you wanted!)
- “out of range” is pretty clear: something is not within the expected range (probably that of
- “The literal”: now that’s interesting: what is a literal?
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
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:
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
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
Note that a similar problem occurs with
Side note: you’ll realize that there are no
short literals and you can still assign values (usually
int literals) to
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
int if the values are within the types range.
L at the end tells Java that
9223372036854775807 is a
I had this problem in the past and I fixed that by writing the value in the scientific form.
double val = 9e300;
long ak = 34778754226788444L/l;
Both use but at a time only one use uppercase L or lowercase l.
Why use L/l? Because long is a part of integral datatype.