106

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;
1
  • By the way: what compiler are you using? Both Eclipse and the Sun JDK compiler give different (in my opinion better) error messages for this problem. Commented Aug 17, 2011 at 13:28

5 Answers 5

221

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

3
  • 5
    It's in the Java language specs. Find the full text here
    – Lukas Eder
    Commented Aug 17, 2011 at 12:59
  • @Lukas, long value= 00000077029062100L; gives me the same error, and it is less than 19 digits. any Idea why it is throwing "The literal 0000007702062100L of type long is out of range "
    – Brooklyn99
    Commented Nov 22, 2019 at 21:05
  • 2
    @SantosshKumhar: That's an octal literal, which cannot contain digits 8 or 9. Remove the leading zeroes.
    – Lukas Eder
    Commented Nov 23, 2019 at 16:31
59

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!)
  • "out of range" is pretty clear: something is not within the expected range (probably that of int)
  • "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 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.

4
  • 5
    Where do you take the time from ;-)
    – Lukas Eder
    Commented Aug 17, 2011 at 14:00
  • 3
    @Lukas: I occasionally write such answers in the hope that I won't have to write 300 shorter ones for it ;-) Also: helping to interpret the error message (hopefully) means less "what does that error message mean" questions. Commented Aug 17, 2011 at 14:01
  • your answer is very good... no doubt, that's why up-vote, but please edit it and at top(start of your answer), just write in 1 line the exact solution to the problem, and then below your explanation. Because lot of people search for quick solution rather than deep explanation Commented Mar 12, 2015 at 12:23
  • 5
    They are free to look at other second answers (the highest-voted one even gives the quick solution). I think that educating people on how to parse and understand error messages is more useful in the long run than just doing it for them. "Give a man a fish..." and all that. Commented Mar 12, 2015 at 13:25
24

Try doing 9223372036854775807L. The L at the end tells Java that 9223372036854775807 is a long.

0
0

I had this problem in the past and I fixed that by writing the value in the scientific form. for example:

double val = 9e300;
-3
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.

Not the answer you're looking for? Browse other questions tagged or ask your own question.