Integer==陷阱

Part 1.创建Integer
//话不多说,Integer==1;与Integer j=1000;源码执行如下
  public static Integer valueOf(int i) {
        if (i >= IntegerCache.low && i <= IntegerCache.high)
            return IntegerCache.cache[i + (-IntegerCache.low)];
        return new Integer(i);
    }
private static class IntegerCache {
        static final int low = -128;
        static final int high;
        static final Integer cache[];

        static {
            // high value may be configured by property
            int h = 127;

如此,关于Integer i =1; Integer j=1;这两个数相等或者Integer i=1000;是否相等的问题就一目了然了
Part 2.Integer运算 Integer i=200; Integer i2=300; Integer i3=500; i3==i+i2的结果肯定是true
运算会涉及到自动拆装箱,进行的是值得比较

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值