public static void main(String[] args) { Integer a = new Integer(3); Integer b = 3; // 将3自动装箱成Integer类型 int c = 3; // b、c地址相同 System.out.println(System.identityHashCode(a));//356573597 System.out.println(System.identityHashCode(b));//1735600054 System.out.println(System.identityHashCode(c));//1735600054 System.out.println(a == b); // false 两个引用没有引用同一对象 System.out.println(a == c); // true a自动拆箱成int类型再和c比较 System.out.println(b == c); // true Integer a1 = 128; Integer b1 = 128; int c1 = 128; // a1、b1、c1 地址不同 System.out.println(System.identityHashCode(a1));//21685669 System.out.println(System.identityHashCode(b1));//2133927002 System.out.println(System.identityHashCode(c1));//1836019240 System.out.println(a1 == b1); // false System.out.println(a1 == c1); // true Integer a2 = 127; Integer b2 = 127; int c2 = 127; // a2、b2、c2 地址相同 System.out.println(System.identityHashCode(a2));//325040804 System.out.println(System.identityHashCode(b2));//325040804 System.out.println(System.identityHashCode(c2));//325040804 System.out.println(a2 == b2); // true System.out.println(a2 == c2); // true }
07-25
242
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)