乘以长值? [英] Multiplying long values?

查看:86
本文介绍了乘以长值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

class Main {  
   public static void main (String[] args){  
     long value = 1024 * 1024 * 1024 * 80;  
     System.out.println(Long.MAX_VALUE);  
     System.out.println(value);  
  }  
}

输出为:


9223372036854775807
0

如果 long值= 1024 * 1024 * 1024 * 80L; !

推荐答案

在Java中,所有数学以处理所有当前值所需的最大数据类型完成。所以,如果你有int * int,它总是将数学作为一个整数,但是int * long是一个long。

In Java, all math is done in the largest data type required to handle all of the current values. So, if you have int * int, it will always do the math as an integer, but int * long is done as a long.

在这种情况下,1024 * 1024 * 1024 * 80作为Int完成,溢出int。

In this case, the 1024*1024*1024*80 is done as an Int, which overflows int.

L当然强制其中一个操作数为Int-64(长)因此,所有数学运算都将值存储为Long,因此不会发生溢出。

The "L" of course forces one of the operands to be an Int-64 (long), therefore all the math is done storing the values as a Long, thus no overflow occurs.

这篇关于乘以长值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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