Multiplying long values?

前端 未结 5 991
盖世英雄少女心
盖世英雄少女心 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:34

    The integer literals are ints. The ints overflow. Use the L suffix.

    long value = 1024L * 1024L * 1024L * 80L;
    

    If the data came from variables either cast or assign to longs beforehand.

    long value = (long)a * (long)b;
    
    long aL = a;
    long bL = b;
    long value = aL*bL
    

    Strictly speaking you can get away with less suffices, but it's probably better to be clear.

    Also not the lowercase l as a suffix can be confused as a 1.

提交回复
热议问题