将 long 解析为负数
代码:
public class Main{
public static void main(String[] a){
long t=24*1000*3600;
System.out.println(t*25);
System.out.println(24*1000*3600*25);
}
}
这打印:
2160000000
-2134967296
为什么?
感谢您的所有回复。
是在数字后面使用 L 的唯一方法吗?
我尝试过 (long)24*1000*3600*25
但这也是负面的。
code:
public class Main{
public static void main(String[] a){
long t=24*1000*3600;
System.out.println(t*25);
System.out.println(24*1000*3600*25);
}
}
This prints :
2160000000
-2134967296
Why?
Thanks for all the replies.
Is the only way to use L after the number?
I have tried the (long)24*1000*3600*25
but this is also negative.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
您已达到
int
类型的最大值,即Integer.MAX_VALUE
或 2^31-1。因此它包裹起来,从而显示负数。有关此内容的即时说明,请参阅此漫画:
You reached the max of the
int
type which isInteger.MAX_VALUE
or 2^31-1. It wrapped because of this, thus showing you a negative number.For an instant explanation of this, see this comic:
为了解释清楚,
上面的语句实际上是
int
文字。要将它们视为long
文字,您需要在它们后面加上L
后缀。需要注意的是,一个小的
l
也足够了,但有时看起来像大写的I
或1
。大写的I
在这里没有多大意义,但是将其读作1
确实会很困难。此外,即使使用L
满足单个值也会使结果long
。To explain it clearly,
In the above statement are actually
int
literals. To make treat them as along
literal you need to suffix those withL
.Caveat, a small
l
will suffice too, but that looks like capitalI
or1
, sometimes. CapitalI
doesn't make much sense here, but reading that as1
can really give hard time. Furthermore, Even sufficing a single value withL
will make the resultlong
.在第一种情况下,您将打印
long
,但在第二种情况下,您将其打印为int
。int
的范围为:-2^31 到 2^31 - 1,它正好低于您正在计算的值(int max:2147483647 you:2160000000),因此您将 int 溢出到负范围。您也可以强制第二个使用
long
:In the first case you are printing a
long
but in the second, you are printing it asint
.And
int
has a range from: -2^31 to 2^31 - 1 which is just below what you are calculating (int max: 2147483647 you: 2160000000) so you overflow the int to the negative range.You can force the second one to use
long
as well:您应该在数字后加上“l”。检查下面的代码片段:
You should suffix the numbers with 'l'. Check the snippet below:
默认情况下,整数文字被视为
int
类型。24*1000*3600*25
大于Integer.MAX_VALUE
,因此会溢出并计算为 -2134967296。您需要使用 L 后缀显式地将其中之一设为long
才能获得正确的结果:Integral literals are treated as type
int
by default.24*1000*3600*25
is greater thanInteger.MAX_VALUE
so overflows and evaluates to -2134967296. You need to explicitly make one of them along
using the L suffix to get the right result:如果您想对大数值进行数学运算而不发生溢出,请尝试使用 BigDecimal 类。
假设我想乘以
200,000,000 * 2,000,000,000,000,000,000L * 20,000,000
运算的值将是 -4176287866323730432,这是不正确的。
通过使用 BigDecimal 类,您可以消除丢失的位并获得正确的结果。
使用 BigDecimal 后,乘法返回正确的结果,即
80000000000000000000000000000000000
If you want to do mathematical operations with large numerical values without over flowing, try the BigDecimal class.
Let's say I want to multiply
200,000,000 * 2,000,000,000,000,000,000L * 20,000,000
The value of the operation will be -4176287866323730432, which is incorrect.
By using the BigDecimal class you can eliminate the dropped bits and get the correct result.
After using the BigDecimal, the multiplication returns the correct result which is
80000000000000000000000000000000000