为什么要施放一个很大的长量int给我们带来一个奇怪的输出(Java)?
我在爪哇练习一些铸件,并且面临着一个我在任何地方找不到任何答案的情况。有很多类似的问题带有答案,但是没有一个给我解释了这种特殊情况。
当我做类似
long l = 165787121844687L;
int i = (int) l;
System.out.println("long: " + l);
System.out.println("after casting to int: " + i);
输出的事情时,
long: 165787121844687
after casting to int: 1384219087
这个结果对我来说非常有趣。
我知道 long 的类型是64位整数, int 类型是32位整数。我还知道,当我们将较大类型的类型投入到较小的类型时,我们可能会丢失信息。而且我知道有一个非常有用的 Math.tointexact()方法。
但是,对此“ 1384219087 ”的解释是什么?数据丢失,但是为什么这个数字呢?如何“ 165787121844687 ”成为“ 1384219087 ”?为什么代码甚至编译?
就是这样。谢谢!
I was practicing some castings in Java and I faced a situation for which I couldn't find any answers, anywhere. There are a lot of similar questions with answers, but none gave me an explanation for this particular case.
When I do something like
long l = 165787121844687L;
int i = (int) l;
System.out.println("long: " + l);
System.out.println("after casting to int: " + i);
The output is
long: 165787121844687
after casting to int: 1384219087
This result is very intriguing for me.
I know that the type long is a 64-bit integer, and the type int is a 32-bit integer. I also know that when we cast a larger type to a smaller one, we can lose information. And I know that there is a Math.toIntExact() method that is quite useful.
But what's the explanation for this "1384219087" output? There was loss of data, but why this number? How "165787121844687" became "1384219087"? Why does the code even compile?
That's it. Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
十六进制符号中的16578712184687L = 0000 96C8 5281 81CF
1384219087在六角符号中= 5281 81CF,
因此,铸件截断了预期的前32位。
165787121844687L in hex notation = 0000 96C8 5281 81CF
1384219087 in hex notation = 5281 81CF
So the cast truncated the top 32 bits as expected.
如果您将这两个数字转换为十六进制,您会
看到这里发生了什么?
If you convert these two numbers to hexadecimal, you get
See what's happened here?
OldProgrammer的答案是正确的,应接受。这是一些其他信息,还有解决方法。
Java规格说这是如此
当您在Java中投放数字原始性时,您对结果负责,包括信息丢失的风险。
为什么?因为Java规格是这样说的。总是最好阅读文档。直觉编程是有风险的业务。
请参阅 java语言规范 href =“ https://docs.oracle.com/javase/specs/jls/se18/html/jls-5.html#jls-5.1.3” rel =“ nofollow noreferrer”> 5.1.3。缩小原始转换 。引用(强调我的):
数学#…精确…
想在从
long
转换为简短
,使用数学
精确的方法。如果操作溢出,则执行。您可以陷入此例外。您会发现类似的
数学#…精确…
绝对值,加法,减少,增加,乘以,负和减法的方法。The Answer by OldProgrammer is correct, and should be accepted. Here is some additional info, and a workaround.
Java spec says so
When you cast a numeric primitive in Java, you take responsibility for the result including the risk of information loss.
Why? Because the Java spec says so. Always best to read the documentation. Programming by intuition is risky business.
See the Java Language Specification, section 5.1.3. Narrowing Primitive Conversion. To quote (emphasis mine):
Math#…Exact…
When you want to be alerted to data loss during conversion from a
long
to ashort
, use theMath
methods for exactitude. If the operation overflows, an execution is thrown. You can trap for that exception.You will find similar
Math#…Exact…
methods for absolute value, addition, decrementing, incrementing, multiplying, negating, and subtraction.