2008-03-28
(三)长除法
//计算一毫秒等于多少微秒
public void computation(){
final long MICROS_PER_DAY = 24 * 60 * 60 * 1000 * 1000;
final long MILLIS_PER_DAY = 24 * 60 * 60 * 1000;
System.out.println(MICROS_PER_DAY / MILLS_PER_DAY);
}
打印值是多少?错了,输出值为5。Java中Integer最长为2147483648,24 * 60 * 60 * 1000 * 1000的结果超出了2147483648,所以MICROS_PER_DAY值为减去2147483648整数倍的余数,然后再去除MILLIS_PER_DAY。方法改为:
public void computation(){
final long MICROS_PER_DAY = 24 * 60 * 60 * 1000 * 1000l;
final long MILLIS_PER_DAY = 24 * 60 * 60 * 1000l;
System.out.println(MICROS_PER_DAY / MILLIS_PER_DAY);
}
打印出的结果为:1000。在做长整数操作的时候,尽量在值后面加上”L/l”或用它的包装类型。
public void computation(){
final long MICROS_PER_DAY = 24 * 60 * 60 * 1000 * 1000;
final long MILLIS_PER_DAY = 24 * 60 * 60 * 1000;
System.out.println(MICROS_PER_DAY / MILLS_PER_DAY);
}
打印值是多少?错了,输出值为5。Java中Integer最长为2147483648,24 * 60 * 60 * 1000 * 1000的结果超出了2147483648,所以MICROS_PER_DAY值为减去2147483648整数倍的余数,然后再去除MILLIS_PER_DAY。方法改为:
public void computation(){
final long MICROS_PER_DAY = 24 * 60 * 60 * 1000 * 1000l;
final long MILLIS_PER_DAY = 24 * 60 * 60 * 1000l;
System.out.println(MICROS_PER_DAY / MILLIS_PER_DAY);
}
打印出的结果为:1000。在做长整数操作的时候,尽量在值后面加上”L/l”或用它的包装类型。
发表评论
最新评论
-
(二)找零钱
1) public String calTimeOfChange(){ java ...
-- by xiquwgugou -
(一)判断奇数
试过,可以实现
-- by mac521 -
(一)判断奇数
public Boolean isOdd(int i){ return (i ...
-- by arthur0592







评论排行榜