包装类型

包装类型

intInteger的区别

这里我们以intInteger来介绍下基本类型与包装类型的区别。首先,int是基本数据类型,int变量存储的是数值。Integer是引用类型,实际是一个对象,Integer存储的是引用对象的地址。Integer对象会占用更多的内存。Integer是一个对象,需要存储对象的元数据。但是int是一个原始类型的数据,所以占用的空间更少。

Integer i = new Integer(100);
Integer j = new Integer(100);
System.out.print(i == j); //false

因为new生成的是两个对象,其内存地址不同。非new生成的Integer变量与new Integer()生成的变量比较,结果为false

/**
 * 比较非new生成的Integer变量与new生成的Integer变量
 */
public class Test {
    public static void main(String[] args) {
        Integer i= new Integer(200);
        Integer j = 200;
        System.out.print(i == j);
        //输出:false
    }
}

因为非new生成的Integer变量指向的是java常量池中的对象,而new Integer()生成的变量指向堆中新建的对象,两者在内存中的地址不同。所以输出为false。两个非new生成的Integer对象进行比较,如果两个变量的值在区间[-128,127]之间,比较结果为true;否则,结果为false

/**
 * 比较两个非new生成的Integer变量
 */
public class Test {
    public static void main(String[] args) {
        Integer i1 = 127;
        Integer ji = 127;
        System.out.println(i1 == ji);//输出:true
        Integer i2 = 128;
        Integer j2 = 128;
        System.out.println(i2 == j2);//输出:false
    }
}

Java在编译Integer i1 = 127时,会翻译成Integer i1 = Integer.valueOf(127)Integer变量(无论是否是new生成的)int变量比较,只要两个变量的值是相等的,结果都为true

/**
 * 比较Integer变量与int变量
 */
public class Test {
    public static void main(String[] args) {
        Integer i1 = 200;
        Integer i2 = new Integer(200);
        int j = 200;
        System.out.println(i1 == j);//输出:true
        System.out.println(i2 == j);//输出:true
    }
}

包装类Integer变量在与基本数据类型int变量比较时,Integer会自动拆包装为int,然后进行比较,实际上就是两个int变量进行比较,值相等,所以为true

自动包装与拆包

int类型在赋值到Integer类时,会自动封装,调用IntegervalueOf(int i)方法。

Integer a = 1;
Integer a = Integer.valueOf(1);
/**
 * Returns an {@code Integer} instance representing the specified
 * {@code int} value.  If a new {@code Integer} instance is not
 * required, this method should generally be used in preference to
 * the constructor {@link #Integer(int)}, as this method is likely
 * to yield significantly better space and time performance by
 * caching frequently requested values.
 *
 * This method will always cache values in the range -128 to 127,
 * inclusive, and may cache other values outside of this range.
 *
 * @param  i an {@code int} value.
 * @return an {@code Integer} instance representing {@code i}.
 * @since  1.5
 */
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);
}

i >= -128 && i <= 127时,Integer.valueOf(i)会将i存储在内部类IntegerCachestatic final Integer cache[]里,这一字节的缓存内存地址是静态的,返回值即:

IntegerCache.cache[i + (-IntegerCache.low)]

因此:

Integer a = 1;
Integer b = 1;

ab的引用都指向同一个对象,即a == b

上一页
下一页