在jdk1.5以上的版本中,基本类型和封装类能自动转化,与String类型的对象和字符串常量类似。
Integer i1 = 123; Integer i2 = 123 int i = 123; Integer i3 = new Integer(123); Integer i4 = new Integer(123); System.out.println("i1 == i2 = "+(i1 == i2)); System.out.println("i1.equals(i2) = "+(i1.equals(i2))); System.out.println(); System.out.println("i3 == i4 = "+(i3 == i4)); System.out.println("i3.equals(i4) = "+(i3.equals(i4))); System.out.println(); System.out.println("i2 == i4 = "+(i2 == i4)); System.out.println("i2.equals(i4) = "+(i2.equals(i4))); System.out.println(); System.out.println("i == i2 = "+(i == i2)); System.out.println("i1.equals(i) = "+(i1.equals(i)));
System.out.println(); System.out.println("i == i4 = "+(i == i4)); System.out.println("i4.equals(i) = "+(i4.equals(i)));
i1 == i2 = true i1.equals(i2) = true
i3 == i4 = false i3.equals(i4) = true
i2 == i4 = false i2.equals(i4) = true
i == i2 = true i1.equals(i) = true
i == i4 = true i4.equals(i) = true