class B
{
 private int i;
 B(int i)
  {
     this.i=i;
   }
 }
public class Ex1 {
    public static void main(String[] args)
    {
        B b1=new B(20);
        B b2=new B(20);
        System.out.println(b1==b2);//b1==b2比较是内存地址 
        
    }

}
View Code
class B
{
 private int i;
 B(int i)
 {
     this.i=i;
 }
 public boolean equals(B b2)//覆盖equals方法
 {
    if(this.i==b2.i)
        return true;
    else
        return false;
 }
}
public class Ex1 {
    public static void main(String[] args)
    {
        B b1=new B(20);
        B b2=new B(20);
        //System.out.println(b1==b2);
        //b1==b2比较是内存地址 
        System.out.println(b1.equals(b2));
        //b1.equals(b2)默认比较内存地址,需覆盖方法
        //equals方法默认object类 
        
    }

}
View Code