zoukankan      html  css  js  c++  java
  • 记一次*的 Integer 溢出

    一不注意, 看起来ok 的地方都出问题了! 后来才突然想起,原来是 Integer 溢出啊

    这样的代码, 没想到也会出问题。
    public static final long Cap_Size = 95100010001000; 而
    Cap2_Size = 98
    100010001000 变成了负数, 真是的。

    public class TestDigitOverflow {

    public static final long Cap_Size = 95*1000*1000*1000; // 95 GB , 1000 作为单位
    public static final long Cap2_Size = 98*1000*1000*1000; // 98 GB , 1000 作为单位 
    public static final long Max_Size = 100*1000*1000*1000; // 100 GB , 1000 作为单位
    
    /**
     * Integer 溢出测试 ! 
     * 
     * @param args
     */
    public static void main(String[] args) {
    	
        System.out.println(Cap_Size);
        System.out.println(Cap2_Size);
        System.out.println(Max_Size);
        
        if (12344 > Cap2_Size) {
            System.err.println( "aaaaaaaaaaaaa " + Long.MAX_VALUE);
        }
    
        System.out.println(95*1000*1000*1000);
        System.out.println(98*1000*1000*1000);
        System.out.println(99*1000*1000*1000);
        System.out.println(100*1000*1000*1000);
        
        /**
         * 加上 L 就好了! 
         */
        System.out.println(95*1000*1000*1000L);
        System.out.println(98*1000*1000*1000L);
        System.out.println(99*1000*1000*1000L);
        System.out.println(100*1000*1000*1000L);
    
    }
    

    }

    结果:

    510719488
    aaaaaaaaaaaaa 9223372036854775807
    -784247808
    1215752192
    510719488
    -784247808
    215752192
    1215752192
    95000000000
    98000000000
    99000000000
    100000000000

  • 相关阅读:
    flutter setInitialRoute: 不生效
    mac os Catalina beta andriod studio crash
    Flutter 集成到现有iOS工程
    理解git
    selenium(一)--selenium 家族
    异常(一)
    java设计模式--创建型模式(一)
    理解JAVA虚拟机(下)
    mockito框架
    三次握手与四次释放
  • 原文地址:https://www.cnblogs.com/FlyAway2013/p/6415423.html
Copyright © 2011-2022 走看看