一、首先需要了解的几个前提
1、自动装箱过程是通过调用valueOf方法实现(如Integer.valueOf(10)),而拆箱过程是通过调用包装器的 xxxValue方法实现(如Integer.intValue(a))。
例如代码:
Integer a = 1;
a++;
其自动装箱和拆箱的过程如下:
0: iconst_1 1: invokestatic #16 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer; 4: astore_1 5: aload_1 6: invokevirtual #22 // Method java/lang/Integer.intValue:()I 9: iconst_1 10: iadd 11: invokestatic #16 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
2、缓存
Integer、Short、Byte、Character、Long包装类型有缓存机制(cache数组)。
Boolean类型有TRUE 和 FALSE两个静态成员。
public static final Boolean TRUE = new Boolean(true);
/**
* The <code>Boolean</code> object corresponding to the primitive
* value <code>false</code>.
*/
public static final Boolean FALSE = new Boolean(false);
Double和Float没有缓存机制。
二、关键的总结写在前面
1、 我们知道,"=="运算符既可以用来比较基本类型变量和引用类型变量。当两个操作数都是包装器类型的变量时,判定标准为他们是否指向同一个对象;而如果其中有一个操作数是表达式(即包含算术运算)则会先进行自动拆箱,再进行对应基本类型变量比较。
2、基本包装类型重写了equals方法,基本包装类型的equals不会进行类型的转换,类型不同的包装类型的变量直接被判定为false,尽管他们的数值有可能是相等的。
三、从一段代码开始
package com.demo;
public class Interview {
public static void main(String[] args) {
// TODO Auto-generated method stub
int i = 3;
Integer a = 1;
Integer b = 2;
Integer c = 3;
Integer d = 3;
Integer e = 250;
Integer f = 250;
Long g = 3L;
Long h = 2L;
System.out.println(i==(a+b));
System.out.println(c==d);
System.out.println(e==f);
System.out.println(c.equals(d));
System.out.println(e.equals(f));
System.out.println(c==(a+b));
System.out.println(c.equals(a+b));
System.out.println(g==(a+b));
System.out.println(g.equals(c));
System.out.println(g.equals(a+b));
System.out.println(g.equals(a+h));
}
}
四、运行结果
true true false true true true true true false false true