Multiplying long values?

前端 未结 5 987
盖世英雄少女心
盖世英雄少女心 2020-12-03 14:08
class Main {  
   public static void main (String[] args){  
     long value = 1024 * 1024 * 1024 * 80;  
     System.out.println(Long.MAX_VALUE);  
     System.out.         


        
5条回答
  •  攒了一身酷
    2020-12-03 14:32

    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.

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

    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.

提交回复
热议问题