这是关于Java中自动装箱与拆箱的一段代码:
public class BoxingTest {
public static void main(String[] args) {
Integer a = 1;
Integer b = 2;
Integer c = 3;
Integer d = 3;
Integer e = 321;
Integer f = 321;
Long g = 3L;
System.out.println(c == d); //true
System.out.println(e == f); //false
System.out.println(c == (a + b)); //true
System.out.println(c.equals(a + b)); //true
System.out.println(g == (a + b)); //true
System.out.println(g.equals(a + b)); //false
}
}
代码注释是实际执行结果,注意:Java的自动装箱(valueOf 方法)与拆箱(intValue 方法,以Integer类型为例)是Java源码编译期间发生的动作,是JDK1.5之后编译器提供的一种语法糖 ,以Integer类型为例,我们看下Java源码中装箱与拆箱的源码实现:
装箱代码,注意IntegerCache的存在:
public static Integer valueOf(int i) {
if (i >= IntegerCache.low && i <= IntegerCache.high)
return IntegerCache.cache[i + (-IntegerCache.low)];
return new Integer(i);
}
拆箱代码:
public int intValue() {
return value;
}
为了更好的理解上面的执行结果,我们可以先看下编译完的class 文件,再重新反编译后的代码是怎么样的:
public class BoxingTest
{
public static void main(String[] args)
{
Integer a = Integer.valueOf (1 )
Integer b = Integer.valueOf (2 )
Integer c = Integer.valueOf (3 )
Integer d = Integer.valueOf (3 )
Integer e = Integer.valueOf (321 )
Integer f = Integer.valueOf (321 )
Long g = Long.valueOf (3 L)
System.out .println (c == d)
System.out .println (e == f)
System.out .println (c.intValue () == a.intValue () + b.intValue ())
System.out .println (c.equals (Integer.valueOf (a.intValue () + b.intValue ())))
System.out .println (g.longValue () == a.intValue () + b.intValue ())
System.out .println (g.equals (Integer.valueOf (a.intValue () + b.intValue ())))
}
}
我们简单分析下上面的代码,为了方便理解,蓝色代码是Java编译器编译后的代码,我们可以看下源码经过编译器的装箱、拆箱操作变成了什么样子:
System.out.println(c == d) => System.out.println(c == d) : 类型一致,不涉及装箱与拆箱,变量c、d保存于线程私有栈中,c和d是保存的是一个地址引用,指向内存堆中的某个地址,对于值在【-128,127】Integer类型,由于IntegerCache.cache[]的存在,所以c、d指向的是堆中同一个地址,故执行结果为true
System.out.println(e == f) => System.out.println(e == f) : 类型一致,不涉及装箱与拆箱操作,判定规则参照上面的分析,因为321不在【-128,127】内,故e和f指向的是内存堆中两个不同的地址,故执行结果为false
System.out.println(c == (a + b)) => System.out.println(c.intValue() == a.intValue() + b.intValue()) : 通过反编译字节码我们可以看到,这里涉及了包装类型的拆箱操作,只有基础类型才可以进行加法操作,实际比较的是存于Java私有线程栈中两个int类型的数值比较,执行结果为true
System.out.println(c.equals(a + b)) => System.out.println(c.equals(Integer.valueOf(a.intValue() + b.intValue()))) : 这里涉及了先拆箱,然后再装箱的操作,a+b先执行拆箱操作,然后再对结果执行装箱操作,最后执行equals方法,我们看下Integer类中equals方法的定义如下,很明显,执行结果为true
public boolean equals(Object obj) {
if (obj instanceof Integer) {
return value == ((Integer)obj).intValue();
}
return false;
}
System.out.println(g == (a + b)) => System.out.println(g.longValue() == a.intValue() + b.intValue()) : 类型不一致,拆箱操作,g和a、b分别执行了拆箱操作,然后比较结果,执行结果为true
System.out.println(g.equals(a + b)) => System.out.println(g.equals(Integer.valueOf(a.intValue() + b.intValue()))) : 与第四条规则一致,先拆箱执行加法操作,然后加法结果执行装箱操作,最后执行Long类型的equals方法,Long中equals方法定义如下,故执行结果为false
public boolean equals(Object obj) {
if (obj instanceof Long) {
return value == ((Long)obj).longValue();
}
return false;
}
集合中只能包含对象,不能包含基础数据类型,如果将基础数据类型的数据添加到集合操作,JVM(JDK1.5之后)会自动进行装箱操作,将基础数据类型封装为对应的封装类
图片链接:https://www.ntu.edu.sg/home/ehchua/programming/java/J5c_Collection.html