1 package wrapper.demo; 2 3 public class WrapperDemo 4 { 5 6 /** 7 * @param args 8 */ 9 public static void main(String[] args)10 {11 12 // JDK1.5以后,自動裝箱,如果裝箱的是一個字節,那么該數據會被共享,不會重新開辟新空間13 Integer a = 127; 14 Integer b = 127;15 System.out.PRintln(a == b);//true16 System.out.println(a.equals(b));//true17 18 Integer c = 128; 19 Integer d = 128;20 System.out.println(c == d);//false 21 System.out.println(c.equals(d));//true22 23 Integer x = new Integer(127);24 Integer y = new Integer(127);25 System.out.println(x == y); //false,因為==是比較對象地址,兩個new,顯然地址不一樣26 System.out.println(x.equals(y));//true,重寫了equals方法,只比較數值是否相等,顯然相等27 28 }29 30 }
新聞熱點
疑難解答