经典指数          
原因
2657
浏览数
0
收藏数
 

以下程序段的输出结果为:
public class EqualsMethod
{
    public static void main(String[] args)
    {
        Integer n1 = new Integer(47);
        Integer n2 = new Integer(47);
        System.out.print(n1 == n2);
        System.out.print(",");
        System.out.println(n1 != n2);
    }
}

  • false,false
  • false,true
  • true,false
  • true,true

     举报   纠错  
 
切换
1 个答案

有些人说的看源码选C,这里有个误区所谓的缓存,是在使用valueOf的时候使用的,

Integer n3 = Integer.valueOf(47)

Integer n4 = Integer.valueOf(47);

这里的n3==n4才是true。

如果直接使用new一个对象,从构造函数来看:

    public Integer(int value) {

        this.value = value;

    }

就是完全不同的两个对象,没有使用所谓的缓存。

 
切换
撰写答案