0
点赞
收藏
分享

微信扫一扫

包装类自动装箱问题

seuleyang 2022-03-12 阅读 29
java
package MONA.demo09_包装类;

public class Demo02{
    public static void main(String[] args) {
        Integer i1 = new Integer(1);
        Integer i2 = new Integer(1);

        //fales  判断的是地址的值
        System.out.println(i1 == i2);

        //true  判断的是实际的值
        System.out.println(i1.equals(i2));

        //自动装箱
        //在jdk1.5自动装箱时,如果数值在byte范围之内,不会新创建对象空间而是使用原来已有的空间
        //byte类型的取值范围为-128到127
        Integer i3 = 100;//这行代码等于Integer i3 = new Integar(value:100)
        Integer i4 = 100;//此100会使用i3的空间
        //本应该是false,但是i3和i4使用的是同一个空间,故地址值一样,为true
        System.out.println(i3 == i4);
        //true
        System.out.println(i3.equals(i4));

        Integer i5 = 666;
        Integer i7 = 666;//此555会使用新的空间
        //false
        System.out.println(i5 == i7);
        //true
        System.out.println(i5.equals(i7));
    }
}
举报

相关推荐

包装箱类的equals方法

int和Integer的拆箱装箱 && 包装类

0 条评论