package sfk.bbs.test.springjsbctempletTest; import static org.junit.Assert.*; import org.junit.Test; public class testBase { @Test public void test() { Integer tt2 = -129; Integer tt = new Integer(-129);//等價於Integer tt2 = -129;,因為不在常量池[-128,127]范圍內所以Integer tt2 = -129;相當於new了一個新的integer對象
System.out.println(tt == tt2);//這里比較的是兩個地址,因為是new的integer對象,所以有新的內存地址.所以他們兩個不相等.==在基本類型比較時比較的是值,在包裝類比較是比較的是內存地址 System.out.println(tt.equals(tt2));//equals方法被重寫了,所以比較的是兩個對象自定拆箱以后成為int之后比較的,兩者的int值是相等的.所以返回true System.out.println(tt > tt2);//當比較大小時自動拆箱為int型, System.out.println(tt < tt2); //System.out.println(tt + tt2); /** * false * true * false * false */ } }
增強版
package sfk.bbs.test.springjsbctempletTest; import org.junit.Test; public class testBase { @Test public void test() { /*源碼 * public static Integer valueOf(int i) { if (i >= IntegerCache.low && i <= IntegerCache.high) return IntegerCache.cache[i + (-IntegerCache.low)]; return new Integer(i); }*/ //自動裝箱 //系統把一個[-128,127]之間的整數自動裝箱成Integer實例,並放入了一個名為cache的數組中緩存起來,如果以后把一個[-128,127]之間的整數自動裝箱成一個integer實例時, //實際上是直接指向對應的數組元素.因此a出的代碼打印為true Integer autoPackage0 = Integer.valueOf(1); /** * public static Integer valueOf(int i) { if (i >= IntegerCache.low && i <= IntegerCache.high) return IntegerCache.cache[i + (-IntegerCache.low)]; return new Integer(i); } */ // 自動裝箱,直接將值賦給一個對象時,調用就是Integer.valueOf(int i)這個方法 // 雙等比較的是對象存在的地址 Integer autoPacking1 = 1; Integer autoPacking2 = 1; Integer newInteger = new Integer(1); System.out.println("在integer的cache中時"); //equeals 比較 System.out.println("autoPacking1.equals(autoPacking2) : "+autoPacking1.equals(autoPacking2));//true 因為兩個對象都是Integer時比較的是自動拆箱的值 System.out.println("autoPacking1.equals(newInteger) : "+autoPacking1.equals(newInteger));//true,,此時無論二者的值是不是在Integercache里面 //雙等比較 System.out.println("autoPacking1==autoPacking2 : "+(autoPacking1==autoPacking2));//true,考慮自動拆箱 System.out.println("autoPacking1==newInteger : "+(autoPacking1==newInteger));//fasle,new關鍵字的意思是不要當前已經有的,必須新建一個 System.out.println("不在integer的cache中時"); // Integer autoPacking3 = Integer.valueOf(128); Integer autoPacking4 = 128; Integer autoPacking5 = 128; Integer newIntger2 = new Integer(128); System.out.println("autoPacking5==autoPacking4 : "+(autoPacking5==autoPacking4));//a 返回false,已經不在cache中所以自動裝箱,調用Integer.valueOf(int i)方法. System.out.println("autoPacking3==autoPacking4 : "+(autoPacking3==autoPacking4));// 返回false,因為都是新的對象,所以地址都不一樣,都返回false System.out.println("autoPacking4==newIntger2 : "+(autoPacking4==newIntger2));//返回false System.out.println("當Integer類型被用來比較大小(>,<,>=,<=)時會先自動拆箱,然后用他們的value比較"); System.out.println(autoPacking4.intValue()==autoPacking4.intValue());//要用雙等判斷兩個對象的值是否相等時,要調用intValue方法 } @Test public void testString() { /** * String 的equals方法中先比較兩個對象的地址是否相等,如果地址相等,那么直接返回true, * 如果地址不相等,遍歷字符傳,然后判斷這兩個對象的字符串的值是否相等,如果此時值相等,此時返回true public boolean equals(Object anObject) { if (this == anObject) { return true; } if (anObject instanceof String) { String anotherString = (String)anObject; int n = value.length; if (n == anotherString.value.length) { char v1[] = value; char v2[] = anotherString.value; int i = 0; while (n-- != 0) { if (v1[i] != v2[i]) return false; i++; } return true; } } return false; } */ System.out.println("提前聲明一下,無論什么時候new 一個對象他要表達的意思都是我不要已經有的對象,必須新建一個對象,所以雙等時二者時(他們的地址)不會相等的"); String s1 = "qq"; String s11 = "qq"; String s2 = new String("qq"); //雙等 System.out.println("s1==s11 : "+(s1==s11));//相等的原因是用到了java緩存機制 //系統會創建一個內容為qq的String實例,並將這個實例緩存起來,當第二次執行 //String s11 = "qq";代碼時,系統會先檢查緩存中是否有這個一個String實例的內容,與這個"qq" //直接量的字符序列相同,如果在緩存中知道了這樣一個String實例,系統會直接讓這個引用指向這個String實例 //即二者的指向的地址是相等的 System.out.println("s1==s2 : "+(s1==s2)); //equals System.out.println("s1.equals(s11) : "+s1.equals(s11));//true 由equals方法的定義可以知道返回true System.out.println("s1.equals(s11) : "+s1.equals(s11));//true 由equals方法的定義可以知道返回true System.out.println("s1.equals(s11) : "+s1.equals(s11));//true 由equals方法的定義可以知道返回true //String的緩存機制和Integer的常量池是不一樣的 } }
相關文檔 http://bbs.csdn.net/topics/392029500 http://www.cnblogs.com/danne823/archive/2011/04/22/2025332.html
