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:
我目前正在处理Java中的数据类型,如果我理解正确,那么类型long将接受范围为:-9,223,372,036,854到+9,223,372,036,854,775,807之间的值。现在,您可以看到下面我创建了一个名为testLong的长变量,尽管当我插入9223372036854775807作为值时,我得到一个错误声明:
"The literal 9223372036854775807 of the type int is out of range."
“int类型的文字9223372036854775807不在范围之内。”
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;
5 个解决方案
#1
159
Add a capital L
to the end:
最后加一个大写的L:
long value = 9223372036854775807L;
Otherwise, the compiler will try to parse the literal as an int
, hence the error message
否则,编译器将尝试将文字解析为int,从而得到错误消息
#2
40
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.
它不是。您应该学会信任编译器消息(特别是当它们来自健全的、现代的编译器而不是古老的C/ c++编译器时)。虽然他们说的语言有时很难理解,但他们通常不会对你撒谎。
Let's look at it again:
让我们再看一遍:
The literal of int 9223372036854775807 is out of range.
int 9223372036854775807的文字不在范围内。
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.
注意,它没有提到变量testLong或类型long,因此它与初始化无关。问题似乎发生在另一个点上。
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告诉我们他想把某物当作int值(这不是你想要的!)
- "out of range" is pretty clear: something is not within the expected range (probably that of
int
) - “超出范围”是非常清楚的:某些内容不在预期范围内(可能是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.
我将暂时离开这个舒适的列表来讨论文字:文字是代码中有价值的地方。有字符串文字,int文字,类文字等等。每次在代码中显式地提到一个值时,它都是一个文字。
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:
您可以通过使用相同的文字来验证这一点,在上下文中,long和int都是可接受的:
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?
PrintStream。println可以取整型或长型(或几乎任何其他类型)。代码应该没问题,对吧?
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
.
问题是“某些数字”被定义为int文字,因此必须在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
).
如果你想写一个长的文字,那么你必须通过附加L(或小写L)来实现这个显式,但我强烈建议你总是使用大写的变体,因为它更容易阅读,也更难把1搞错。
Note that a similar problem occurs with float
(postfix F
/f
) and double
(postfix D
/d
).
注意,浮动(后缀F/ F)和double(后缀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.
注:你会意识到,没有字节或简短的文字,你仍然可以赋值(通常是int文字)字节和短的变量:可能由于特殊规则关于作业Converson§5.2:它们允许分配常数表达式的类型字节,短,char或int类型范围内的值。
#3
11
Try doing 9223372036854775807L
做9223372036854775807 l
#4
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;
#5
-3
long ak = 34778754226788444L/l;
Both use but at a time only one use uppercase L or lowercase l.
两者都使用,但每次只有一个使用大写L或小写L。
Why use L/l? Because long is a part of integral datatype.
为什么使用L / L ?因为long是整型数据类型的一部分。
#1
159
Add a capital L
to the end:
最后加一个大写的L:
long value = 9223372036854775807L;
Otherwise, the compiler will try to parse the literal as an int
, hence the error message
否则,编译器将尝试将文字解析为int,从而得到错误消息
#2
40
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.
它不是。您应该学会信任编译器消息(特别是当它们来自健全的、现代的编译器而不是古老的C/ c++编译器时)。虽然他们说的语言有时很难理解,但他们通常不会对你撒谎。
Let's look at it again:
让我们再看一遍:
The literal of int 9223372036854775807 is out of range.
int 9223372036854775807的文字不在范围内。
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.
注意,它没有提到变量testLong或类型long,因此它与初始化无关。问题似乎发生在另一个点上。
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告诉我们他想把某物当作int值(这不是你想要的!)
- "out of range" is pretty clear: something is not within the expected range (probably that of
int
) - “超出范围”是非常清楚的:某些内容不在预期范围内(可能是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.
我将暂时离开这个舒适的列表来讨论文字:文字是代码中有价值的地方。有字符串文字,int文字,类文字等等。每次在代码中显式地提到一个值时,它都是一个文字。
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:
您可以通过使用相同的文字来验证这一点,在上下文中,long和int都是可接受的:
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?
PrintStream。println可以取整型或长型(或几乎任何其他类型)。代码应该没问题,对吧?
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
.
问题是“某些数字”被定义为int文字,因此必须在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
).
如果你想写一个长的文字,那么你必须通过附加L(或小写L)来实现这个显式,但我强烈建议你总是使用大写的变体,因为它更容易阅读,也更难把1搞错。
Note that a similar problem occurs with float
(postfix F
/f
) and double
(postfix D
/d
).
注意,浮动(后缀F/ F)和double(后缀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.
注:你会意识到,没有字节或简短的文字,你仍然可以赋值(通常是int文字)字节和短的变量:可能由于特殊规则关于作业Converson§5.2:它们允许分配常数表达式的类型字节,短,char或int类型范围内的值。
#3
11
Try doing 9223372036854775807L
做9223372036854775807 l
#4
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;
#5
-3
long ak = 34778754226788444L/l;
Both use but at a time only one use uppercase L or lowercase l.
两者都使用,但每次只有一个使用大写L或小写L。
Why use L/l? Because long is a part of integral datatype.
为什么使用L / L ?因为long是整型数据类型的一部分。