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

  • 相关阅读:
    JavaBean命名不规范导致数据导出丢失
    Vue应用部署
    vue-cli的lib库模式下调试和不输出map文件
    Vue CLI的理解
    MongoDB副本集原理
    MongDB副本集成员状态
    Appium Android环境搭建
    如何自己实现一个HTMLRunner
    使用Flask搭建基于unittest的简单用例挑选及执行平台
    Django Admin中增加导出Excel功能
  • 原文地址:https://www.cnblogs.com/FlyAway2013/p/6415423.html
Copyright © 2011-2022 走看看