为什么用==比较两个对象的时候是false
用equals比较的时候是true

解决方案 »

  1.   

    ==比较的是对象的内存地址。
    equals比较的是对象的数值。
    自然不一样了。建议比较数值的时候用equals
      

  2.   

    原始数据类型使用 == 判等
    引用类型override equals 判等
      

  3.   


    import java.util.*;public class EqualsTest
    {
    public static void main(String args[]){
    Employee alice1 = new Employee("Alice Adams",75000,1987,12,15);
    Employee alice2 = alice1;
    Employee alice3 = new Employee("Alice Adams",75000,1987,12,15);
    Employee bob = new Employee("Bob Brandson",50000,1989,10,1);

    //System.out.println(alice1);
    //System.out.println(alice2);
    //System.out.println(alice3);

    System.out.println("alice1 == alice2 : " +(alice1 == alice2)); System.out.println("alice1 == alice3 : " +(alice1 == alice3)); System.out.println("alice1.equals(alice3) : " +(alice1.equals(alice3))); System.out.println("alice1.equals(bob) : " +(alice1.equals(bob))); System.out.println("bob.toString: " + bob); Manager car1 = new Manager("Carl Cracker",80000,1987,12,15); Manager boss = new Manager("Carl Cracker",80000,1987,12,15);
    boss.setBonus(5000);
    System.out.println("boss.toString(): "+ boss);
    System.out.println("car1.equals(boss): "+ car1.equals(boss)); System.out.println("alice1.hashCode(): "+ alice1.hashCode());
    System.out.println("alice3.hashCode(): "+ alice3.hashCode());
    System.out.println("bob.hashCode(): "+ bob.hashCode());
    System.out.println("car1.hashCode(): "+ car1.hashCode()); }
    }
    class Employee
    {
    private String name;
    private double salary;
    private Date hireDay;
    public Employee(String n,double s,int year,int month,int day){
    this.name = n;
    this.salary = s;
    GregorianCalendar calendar = new GregorianCalendar(year,month-1,day);
    this.hireDay = calendar.getTime();
    } public String getName(){
    return name;
    }

    public double getSalary(){
    return salary;
    } public Date getHireDay(){
    return hireDay;
    } public void raiseSalary(double byPercent){
    double raise = salary * byPercent/100;
    salary +=raise;
    } public boolean equals(Object otherObject){
    if(this == otherObject) {
    //System.out.println("---------------------------------");
    //System.out.println(this.getClass().getName());
    //System.out.println(this);
    //System.out.println(otherObject.getClass().getName());
    //System.out.println(otherObject);
    //System.out.println("---------------------------------"); return true;
    } if(otherObject == null) return false; if(this.getClass() != otherObject.getClass()) return false; Employee other = (Employee)otherObject; return this.name.equals(other.name) && this.salary == other.salary && this.hireDay.equals(other.hireDay); } public int hashCode(){
    return 7*this.name.hashCode() + 11 * new Double(salary).hashCode() + 13 * this.hireDay.hashCode();
    } public String toString(){
    return this.getClass().getName() + "[name=" + this.name + ",salary="+ this.salary + ",hireDay=" + this.hireDay +"]";
    }
    };
    class Manager extends Employee
    {
    private double bonus;
    public Manager(String n,double s,int year,int month,int day){
    super(n,s,year,month,day);
    bonus = 0;
    } public double getSalary(){
    double baseSalary = super.getSalary();
    return baseSalary + bonus;
    } public void setBonus(double b){
    this.bonus = b;
    } public boolean equals(Object otherObject){
    if(!super.equals(otherObject)) return false; //调用父类中的equals方法
    Manager other = (Manager)otherObject;
    return this.bonus == other.bonus;
    } public int hashCode(){
    return super.hashCode() + 17 * new Double(bonus).hashCode();
    } public String toString(){
    return super.toString() + "[bonus=" + this.bonus+"]";
    }
    }alice1 alice2 alice3 这三个对象的内存地址也是相同的 为什么 用==比较是false 用equals是true 有点不明白了
      

  4.   

    ==是比较两个对象的引用equals是比较两个对象的
      

  5.   

    如果是引用数据类型的话,==比较的是对象的地址。equals()比较的是内容。其实equals()是Object类的一个方法,而所有类都是Object子类,所有类都有equals()这个方法,你可以重写这个方法完成你的比较。String类已经重写过equals()方法,直接可以用来比较String对象内容
      

  6.   

    alice1 alice2 相同 
    alice3 不同
      

  7.   

    lz.首先你的说法就是错误的。
    alice1 alice2 alice3 这三个对象的内存地址也是相同的 为什么 用==比较是false 用equals是true 有点不明白了
    你的alice1和alice2是一样的。alice1和alice2读取的是同一个内存地址。而alice和其他两个值是一样的,但是却是另外new出来的对象,只要new 出来之后,内存地址就不一样的了。多看看堆栈就明白了
      

  8.   

    alice1 alice2 相同 
    alice3 bob 不相同 
    楼主检查一下,是不是自己看错了,如果你就需要验证==和equals()你可以弄个简单的例子,试试就可以。引用实际上就像地址。你可以用System.identityHashCode(Object o)方法求出其特殊HashCode值,如果是不同对象其HashCode码是不同的,无论你怎样重写Object的HashCode方法,System.identityHashCode(Object o)方法都可以辨别出不同对象,也就是说此方法是根据地址求出的HashCode码,楼主可以拿这个方法测试下。
      

  9.   


    Employee@2cd55b63
    Employee@2cd55b63
    Employee@2cd55b63
    这是我打印出来的内存地址分别是alice1 alice2 alice3的
      

  10.   

    恩 的确1和2的相同 3的不同 
    但是用System.out.println(alcie1);
    System.out.println(alcie2);
    System.out.println(alcie3);打出来的结果是这样的Employee@2cd55b63
    Employee@2cd55b63
    Employee@2cd55b63
      

  11.   


    你那是输出的值。值自然是相同的了。
    System.out.println(System.identityHashCode(alice3));
    System.out.println(System.identityHashCode(alice1));
    你这样来判断他们的内存地址,输出就不一样了。
      

  12.   

    Employee@2cd55b63
    Employee@2cd55b63
    Employee@2cd55b63
    那输出来的这些是什么东西了 难懂不是地址码
      

  13.   

    @之前是类名称,之后是HashCode值,你看看你定义类的时候是不是重写了HashCode()方法!最好使用System.identityHashCode(Object o)来判断不同对象~因为这个方法就是根据地址求出的HashCode值,不同的对象绝对值不同。刚才12楼的兄台已经说得很明白了