Integer的比较==和String的比较==总结

坑:

Integer的值为-128-127之间的时候就会自动的从Integer的缓存(IntegerCache)中去取,如果超过这个范围就会新建一个Integer的对象。

  所以a==b(a=10,b=10)为true,c==d(c=150,d=150)为false。

nteger的值为-128-127的时候用==会返回true 不再这个区间要用equals才会返回true

 

 

package com.hzwealth.test.question;

public class IntegerTest {
    public static void main(String[] args) {
        //Integer
        Integer a=10,b=10,c=150,d=150;
        System.out.println(a==b);   
        System.out.println(c==d);
        System.out.println(a.equals(b));
        System.out.println(c.equals(d));
        //String
        String str=new String("AB");
        String str1 ="A";
        String str2 = str1+"B";
        String str3="AB";
        System.out.println(str==str3);
        System.out.println(str==str2);
        System.out.println(str2==str3);
    }
}

 

  1、Integer的问题,首先我们先看上面代码的 a==b会输出什么呢,答案是true,这个毋庸置疑,但是c==d会输出什么呢,答案是false,为什么呢?

    (1)Integer是基本数据类型的int的引用类型,官方语言叫做装箱类型,我们来看一下Integer的源码

 

//Integer的取值
public static Integer valueOf(int i) {
        assert IntegerCache.high >= 127;
        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;
            String integerCacheHighPropValue =
                sun.misc.VM.getSavedProperty("java.lang.Integer.IntegerCache.high");
            if (integerCacheHighPropValue != null) {
                int i = parseInt(integerCacheHighPropValue);
                i = Math.max(i, 127);
                // Maximum array size is Integer.MAX_VALUE
                h = Math.min(i, Integer.MAX_VALUE - (-low));
            }
            high = h;

            cache = new Integer[(high - low) + 1];
            int j = low;
            for(int k = 0; k < cache.length; k++)
                cache[k] = new Integer(j++);
        }

        private IntegerCache() {}
    }

 

  从源码我们可以看出,Integer的值为-128-127之间的时候就会自动的从Integer的缓存(IntegerCache)中去取,如果超过这个范围就会新建一个Integer的对象。

  所以a==b(a=10,b=10)为true,c==d(c=150,d=150)为false。

 2、我们来看一下String的这个问题

  (1)str==str3 为false,因为,str新建了一个对象,str3是常量池中的对象,所以这两个为false。

  (2)str2==str3的解释:

   new 了一个 StringBuilder,然后调用了append方法,对应的就是:String str2 = str1+"B";

  这里还提到了字面量和引用,String str3="AB",这就是字面量,而引用就是,String str=new String("AB");我们自己定义的。

   字面量 jvm 优化过,直接就扔到常量池去了。

  下面是StringBuilder的toString方法,这里也是新建了一个String方法

 

    综上所述,str2==str3也为false。

  补充:这里的“+”不是用的String 的concat的方法,而是用的是StringBuilder来写的。而且,就算是用concat的方法也是new了一个String的对象。下面是concat的源码:

 

public String concat(String str) {
        int otherLen = str.length();
        if (otherLen == 0) {
            return this;
        }
        int len = value.length;
        char buf[] = Arrays.copyOf(value, len + otherLen);
        str.getChars(buf, len);
        return new String(buf, true);
    }

 

  (3)str==str2,这个也毋庸置疑的为false了,因为str是new出来的String对象,str2也是相当于new出来的一个对象。所以为false。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值