1:
public class test1
 {
  public static void main (String args[])
    { 
    Integer i=new Integer(100);
    Integer j=new Integer(100)  
  System.out.println(i.equals(j)); 
  }
  }
2:
class A
  {
void A(int i)
  {
   }
  
}
public class test2
 {
  public static void main (String args[])
    { 
    A i=new A(100);
    A j=new A(100)  
  System.out.println(i.equals(j)); 
  }
  }
为什么两个输出不一样 equals()方法比较的不是句柄指向的内容吗为什么第一个例子输出的是true,第二个输出的是false?

解决方案 »

  1.   

    you need to override the equals method in class A, so that it knows what kind of situation can be considered as "equal"
      

  2.   

    你没有为A提供equals方法。因此使用Object的equals,而Object的equals是直接用==判断的。
      

  3.   

    Integer j=new Integer(100)//少了个;
    A j=new A(100) //少了个;
      

  4.   

    需要覆盖equals()方法,参考如下:public class A {    private int i;
        
        public A(int i) {
            this.i = i;
        }
        
        public boolean equals(Object o) {
            if (o==null || !(o instanceof A))
                return false;
            else
                return this.i == ((A)o).i;
        }
      
    }