int 类型的文字 xyz 超出范围
我目前正在使用 Java 中的数据类型,如果我理解正确的话,类型 long
接受 -9,223,372,036,854,775,808 到 +9,223,372,036,854,775,807 范围之间的值。现在,如下所示,我创建了一个名为 testLong
的 long
变量,尽管当我插入 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
在末尾添加大写的
L
:否则,编译器将尝试将文字解析为
int
,因此会出现错误消息Add a capital
L
to the end:Otherwise, the compiler will try to parse the literal as an
int
, hence the error message它不是。您应该学会信任编译器消息(尤其是当它们来自理智的现代编译器而不是往往会出现错误消息的古老编译器时)。虽然他们所说的语言有时可能难以理解,但他们通常不会对您撒谎。
我们再看一下:
请注意,它没有在任何地方提到您的变量
testLong
或类型long
,所以问题是没有关于初始化。它似乎发生在其他某个时刻。现在让我们研究一下消息的某些部分:
int
告诉我们他想将某些内容视为int
值(这不是您想要的!)int
)我将暂时离开这个舒适的列表来讨论文字:文字是您在代码中具有某些价值的地方。有
String
文字、int
文字、class
文字等。每次您在代码中明确提及一个值时,它都是一个文字。所以它实际上并不是在让你担心变量声明,而是在数字本身、值上让你担心。
您可以通过在
long
和int
同等可接受的上下文中使用相同的文字来轻松验证这一点:PrintStream.println
可以采用一个int
或 along
(或几乎任何其他内容)。那么这段代码应该没问题,对吧?不。嗯,也许应该是这样,但根据规则,它是不好的。
问题是“一些数字" 被定义为
int
文字,因此必须位于int
定义的范围内。如果你想写一个
long
文字,那么你必须通过附加L
(或小写l
)来明确它,但我强烈建议您始终使用大写变体,因为它更容易阅读并且更不易被误认为1
)。请注意,
float
(后缀F
/f
)和double
(后缀D
/d
)。旁注:您会意识到没有
byte
或short
文字,您仍然可以分配值(通常是int 文字)到
byte
和short
变量:由于 § 5.2 关于赋值上下文中的转换:它们允许将较大类型的常量表达式分配给byte
、short
、char
或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:
Note, that it doesn't mention your variable
testLong
or the typelong
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 anint
value (which is not what you wanted!)int
)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 anint
are equally acceptable:PrintStream.println
can take either anint
or along
(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 byint
.If you want to write a
long
literal, then you must make that explicit by appending theL
(or lower casel
, but I highly suggest you always use the upper-case variant, because it's much easier to read and harder to mistake for a1
).Note that a similar problem occurs with
float
(postfixF
/f
) anddouble
(postfixD
/d
).Side note: you'll realize that there are no
byte
orshort
literals and you can still assign values (usuallyint
literals) tobyte
andshort
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 tobyte
,short
,char
orint
if the values are within the types range.尝试执行
9223372036854775807L
。末尾的L
告诉 Java9223372036854775807
是一个long
。Try doing
9223372036854775807L
. TheL
at the end tells Java that9223372036854775807
is along
.我过去遇到过这个问题,我通过以科学形式写出该值来解决这个问题。
例如:
I had this problem in the past and I fixed that by writing the value in the scientific form.
for example:
两者都使用,但一次只有一个使用大写 L 或小写 l。
为什么使用 L/l?因为 long 是整型数据类型的一部分。
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.