Int 类型的文本 xyz 超出范围

我目前使用的是 Java 中的数据类型,如果我理解正确的话,long类型接受 -9,223,372,036,854,775,808到 + 9,223,372,036,854,775,807之间的值。现在你可以看到,我已经创建了一个名为 testLonglong变量,虽然当我插入9223372036854775807作为值时,我得到一个错误说明:

Int 类型的文字9223372036854775807超出了范围。

我不知道为什么它把 long数据类型称为 int

有人知道吗?

密码:

char testChar = 01;
byte testByte = -128;
int testInt = -2147483648;
short testShort = -32768;
long testLong = 9223372036854775807;
float testFoat;
double testDouble = 4.940656458412;
boolean testBool = true;
114166 次浏览

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

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

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 short0 the values are within the types range.

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

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.