为什么输出是真的和假的?

时间:2014-07-08 06:34:41

标签: java

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);
    }
}

1 个答案:

答案 0 :(得分:5)

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

    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
}