为什么这不会导致1000? [英] Why does this not result in 1000?

查看:133
本文介绍了为什么这不会导致1000?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

Java问题 - 背后的原因和可能的输出是什么





long milli=24*60*60*1000;
long micro=24*60*60*1000*1000;
long result=micro/milli;

结果应为1000但不是。当我使用 24 * 60 * 60 * 1000 * 1000L 时为什么会有效?

The result should be 1000 but its not. Why does it work when I use 24*60*60*1000*1000L?

有人可以告诉我原因是什么?

Can someone tell me the reason for this?

推荐答案

问题是计算是在 int s然后转换为 long ,遗憾的是24 * 60 * 60 * 1000 * 1000将不适合 int 并包裹。你可以这样做

The trouble is that the calculation is done on ints and then casted to long, and unfortunately 24*60*60*1000*1000 won't fit in an int and wraps around. You could do something like

long micro=24L*60*60*1000*1000;

强制在longs上完成最后一次乘法。

to force the last multiplication to be done on longs.

这篇关于为什么这不会导致1000?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆