* 總之:
“==”比較的是值【變量(棧)內存中存放的對象的(堆)內存地址】
基本數據類型比較:自然數值。
引用:比較的也是值,只不過不是數值,是變量本身的值(這個值是內存地址)
equals,類型 、值,相同,就返回 true
其實有疑惑,可以用 javap -c EqualsTest2 (反編譯class,class文件存放位置在bin目錄裏),
可以看到底層調用的函數
**/
public class EqualsTest2 {
public static void main(String[] args) {
/**顯然此處創建的對象和new 的對象用的方法是不一樣的。所謂的裝箱調用的是valuof()方法
IntegerCache [-128,127]
public static Integer valueOf(int i) {
if (i >= IntegerCache.low && i <= IntegerCache.high)
return IntegerCache.cache[i + (-IntegerCache.low)];
return new Integer(i);
}
**/
Integer i=1;//裝箱相當於調用Integer.valueOf(1)
Integer j=1;
System.out.println("i==1:"+(i==1)); //拆箱調用 Integer.intValue();
System.out.println("(i==j):"+(i==j));
System.out.println("===========================");
Integer i2=128; //裝箱相當於調用Integer.valueOf(),128不在[-128,127],創建新對象
Integer j2=128;
System.out.println("i2==j2:"+(i2==j2)); //2個對象,肯定是2個不同的內存地址
System.out.println("===========================");
Integer i1=new Integer(1);
Integer j1=new Integer(1);
System.out.println("i1==j1:"+(i1==j1));
}
}