int、Integer、new Integer和Integer.valueOf()的 ==、equals比较

Java 基本数据类型的包装类型的大部分都用到了缓存机制来提升性能。

Byte,Short,Integer,Long 这 4 种包装类默认创建了数值 [-128,127] 的相应类型的缓存数据,Character 创建了数值在 [0,127] 范围的缓存数据,Boolean 直接返回 True or False。

如果超出对应范围仍然会去创建新的对象,缓存的范围区间的大小只是在性能和资源之间的权衡。

两种浮点数类型的包装类 Float,Double 并没有实现缓存机制。

Integer i1 = 33;
Integer i2 = 33;
System.out.println(i1 == i2);// 输出 true

Float i11 = 333f;
Float i22 = 333f;
System.out.println(i11 == i22);// 输出 false

Double i3 = 1.2;
Double i4 = 1.2;
System.out.println(i3 == i4);// 输出 false

一个小示例:

Integer i1 = 40;
Integer i2 = new Integer(40);
System.out.println(i1==i2);

Integer i1=40 这一行代码会发生装箱,也就是说这行代码等价于 Integer i1=Integer.valueOf(40) 。因此,i1 直接使用的是缓存中的对象。而Integer i2 = new Integer(40) 会直接创建新的对象。

因此,答案是 false 。

一、Int a = xx;

两个Int类型变量用“==”比较的是地址,也就是栈中值的大小。

二、Integer a = xx;Integer a = Integer.valueOf();

2.1 Integer a = xx

将Int值赋给Integer变量,系统会自动将这个Int值封装成一个Integer对象。

比如:Integer a = 100;实际上的操作是:Integer a = Integer.valueOf(100);

注意:这里Integer.valueOf(),当Int值的范围在-128-127之间时,会通过一个IntegerCache缓存来创建Integer对象;当Int值不在该范围时,直接调用new Integer()来创建对象,因此会出现以下的情况

public class Main {
    public static void main(String[] args) {
        Integer a = 100;
        Integer aa = 100;
        Integer b = 200;
        Integer bb = 200;
        System.out.println(a == aa);//true
        System.out.println(b == bb);//false
    }
}
  • 基于减少对象创建次数和节省内存的考虑,JVM方法区中缓存了[-128,127]之间的数字。此数字范围内传参则直接返回缓存中的对象,在此之外,直接new出来。

2.2 Integer. valueOf()

可以将基本类型int转换为包装类型Integer,或者将String转换成Integer,String如果为Null或“”都会报错

public class Main {
    public static void main(String[] args) {
        Integer a = 128;
        Integer b = 128;
        Integer c = Integer.valueOf("12");
        System.out.println(a == b);
        System.out.println(c);
    }
}

输出:
在这里插入图片描述
超过127会new一个对象出来就是false不相等了。

2.3 equals比较的值

public static void main(String[] args) {
	Integer i1 = 200;
	Integer i2 = 200;
	
	System.out.println(i1 == i2);
	System.out.println(i1.equals(i2));
}	

equals比较值,输出true:

false 
true

三、new Integer()

不管值的范围是否在-128-127,new Integer()每次都会创建新的对象,通过==比较两个对象的内存地址

public class Main {
    public static void main(String[] args) {
        Integer a = new Integer(100);
        Integer b = new Integer(100);
        System.out.println(a == b);//false
    }
}

四、三者之间的比较

4.1 与int类型比

不管是new创建的Integer对象,还是通过直接赋值Int值创建的Integer对象,它们与Int类型变量通过“==”进行比较时都会自动拆箱变成Int类型,所以Integer对象和Int变量比较的是内容大小。

public class Main {
    public static void main(String[] args) {
        int a = 100;
        Integer b = 100;//等价于b=Integer.valueOf(100);
        Integer c = new Integer(100);
        System.out.println(a == b);//true
        System.out.println(a == c);//true
    }
}

4.2 与new Integer()比

new创建的Integer对象和直接赋Int值创建的Integer对象使用==比较的是它们的内存地址。

  • 不管是否在-128-127的范围,new 都会新建对象,而Integer b=100则会拿缓存
public class Main {
    public static void main(String[] args) {
        Integer b = 100;//等价于b=Integer.valueOf(100);
        Integer c = new Integer(100);
        System.out.println(b == c);//false
    }
}

Integer.valueOf(49)Integer a = 49;在执行的时候是一样的,会自动调用Integer.valueOf,但是它们和new 地址都是不一样的

public class Main {
    public static void main(String[] args) {
        Integer a = 49;
        int b = 49;
        Integer c = Integer.valueOf(49);
        Integer d = new Integer(49);
        System.out.println(a == b);//true
        System.out.println(a == c);//true
        System.out.println(b == c);//true
        System.out.println(c == d);//false
    }
}

参考:Java中Int、Integer、Integer.valueOf()、new Integer()之间的区别
Integer. valueOf()的使用

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值