int 类型的文字 xyz 超出范围

发布于 2024-11-29 22:33:42 字数 620 浏览 1 评论 0原文

我目前正在使用 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;

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;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

说不完的你爱 2024-12-06 22:33:42

在末尾添加大写的 L

long value = 9223372036854775807L;

否则,编译器将尝试将文字解析为 int,因此会出现错误消息

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

吹梦到西洲 2024-12-06 22:33:42

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

它不是。您应该学会信任编译器消息(尤其是当它们来自理智的现代编译器而不是往往会出现错误消息的古老编译器时)。虽然他们所说的语言有时可能难以理解,但他们通常不会对您撒谎。

我们再看一下:

int 9223372036854775807 的文字超出范围。

请注意,它没有在任何地方提到您的变量testLong或类型long,所以问题是没有关于初始化。它似乎发生在其他某个时刻。

现在让我们研究一下消息的某些部分:

  • int 告诉我们他想将某些内容视为 int 值(这不是您想要的!)
  • “out of range” 非常清楚:某些内容不在预期范围内(可能是 int
  • “文字”:现在很有趣:什么是文字?

我将暂时离开这个舒适的列表来讨论文字:文字是您在代码中具有某些价值的地方。有 String 文字、int 文字、class 文字等。每次您在代码中明确提及一个时,它都是一个文字。

所以它实际上并不是在让你担心变量声明,而是在数字本身、值上让你担心。

您可以通过在 longint 同等可接受的上下文中使用相同的文字来轻松验证这一点:

System.out.println(9223372036854775807);

PrintStream.println 可以采用一个inta long (或几乎任何其他内容)。那么这段代码应该没问题,对吧?

不。嗯,也许应该是这样,但根据规则,它是不好的。

问题是“一些数字" 被定义为 int 文字,因此必须位于 int 定义的范围内。

如果你想写一个long文字,那么你必须通过附加L(或小写l)来明确它,但我强烈建议您始终使用大写变体,因为它更容易阅读并且更不易被误认为1)。

请注意,float(后缀 F/f)和 double(后缀 D/d)。

旁注:您会意识到没有 byteshort 文字,您仍然可以分配值(通常是 int 文字)到 byteshort 变量:由于 § 5.2 关于赋值上下文中的转换:它们允许将较大类型的常量表达式分配给 byteshortcharint 如果值都在类型范围内。

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.

一身软味 2024-12-06 22:33:42

尝试执行9223372036854775807L。末尾的 L 告诉 Java 9223372036854775807 是一个 long

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

薄荷梦 2024-12-06 22:33:42

我过去遇到过这个问题,我通过以科学形式写出该值来解决这个问题。
例如:

double val = 9e300;

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

double val = 9e300;
回忆那么伤 2024-12-06 22:33:42
long ak = 34778754226788444L/l;

两者都使用,但一次只有一个使用大写 L 或小写 l。

为什么使用 L/l?因为 long 是整型数据类型的一部分。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文