问题
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 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 Below:
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:
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
回答2:
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 C/C++ compilers). 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 it's not about the initialization. The problem 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 anintvalue (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 Assignment Converson: they allow assignment of constant expressions of a larger type to byte, short, char or int if the values are within the types range.
回答3:
Try doing 9223372036854775807L
回答4:
I had this problem in the past and I fixed that by writing the value in the scientific form. for example:
double val = 9e300;
回答5:
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.
来源:https://stackoverflow.com/questions/7093186/the-literal-xyz-of-type-int-is-out-of-range