10年 Java程序员,硬核人生!勇往直前,永不退缩!

欢迎围观我的git:https://github.com/R1310328554/spring_security_learn 寻找志同道合的有志于研究技术的朋友,关注本人微信公众号: 觉醒的码农,或Q群 165874185

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

一不注意, 看起来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

posted on 2017-02-19 12:04  CanntBelieve  阅读(383)  评论(0编辑  收藏  举报