Java 为什么输出是正确的和错误的?

Java 为什么输出是正确的和错误的?,java,Java,Integer类为-128和127之间的值保留本地缓存。。并返回相同的对象 public class Test { public static void main(String ar[]) { Integer a = 10; Integer b =10; Integer c = 145; Integer d = 145; System.out.println(a==b); System

Integer类为
-128
127
之间的值保留本地缓存。。并返回相同的对象

public class Test
{
    public static void main(String ar[])
    {
        Integer a = 10;
        Integer b =10;
        Integer c = 145;
        Integer d = 145;
        System.out.println(a==b);
        System.out.println(c==d);
    }
}

旁注:可以在HotSpot VM上使用
-XX:AutoBoxCacheMax=
参数更改此缓存的大小,因此
c==d
完全有可能返回
true
。自2004年引入Java 5.0以来,这种情况一直存在。您可能会认为这一点以前已经得到了回答;)@彼得拉维-由几个不同的人回答了好几次P
    Integer a = 10;
    Integer b =10;
    Integer c = 145;
    Integer d = 145;
    System.out.println(a==b); // so, a and b are references to the same object --> prints true
    System.out.println(c==d);// so, c and d are references to different objects --> returns false
}