why equals() method when we have == operator? [dup

2019-01-01 16:24发布

This question already has an answer here:

When i see the implementation of equals() method it does nothing but same as what == does. So my question is what was the need to have this as separate method when we have == operator which does the same work?

8条回答
永恒的永恒
2楼-- · 2019-01-01 16:40

In case of primitives, the == operator checks if two values are the same.
If it aren't primitives, it checks if it are two pointers (or references) pointing to the same instance of an object.

The equals() method performs a custom check, which is in Object checking the reference, by using ==. But in other classes, sometimes equals() is overridden (I don't know if this is a correct past participle). equals() have to check the content.

So, for example:

int i0 = 34;
int i1 = 34;
int i2 = 35;
// results
i0 == i1: true
i1 == i0: true
i2 == i0: false

But if we have non-primitives

String str0 = new String("Hello man!");
String str1 = new String("Hello man!");
String str2 = new String("!nam olleH");
String str2copy = str2;
// Results
str0 == str1: false // Pointer to two different object, so == will give false
str1 == str2: false // Idem
str2 == str2copy: true // So this are two pointers to the same object
str0.equals(str1): true // This are not the same objects, but they are equal
str1 == str1: true // Again: two times a pointer to the same  object

So, why str0.equals(str1) returns true? Because the String class has an override of equals(). And in that method it doesn't check if they are equal by doing return this == obj; But in that method, there is a full check. I don't know which method they use to compare the two strings, but here are two possible ways:

  • Generating from the the two string a hash-code and check if they are equal (int == int)
  • Checking character by character if they are the same.

So I hope this is clear now.

查看更多
泪湿衣
3楼-- · 2019-01-01 16:47

There is a very important difference between the two.

"==" compares object instances. The default equals() implementation does this, also. Please run & analyse the following code sample:

public class Person{
   String name;

   public Person(String name){
       this.name = name;
   }

//overriding equals
public boolean equals( Object obj ) {
    if( this == obj )
        return true;
    if( obj == null )
        return false;
    if( getClass() != obj.getClass() )
        return false;
    Person other = (Person) obj;
    if( name == null ) {
            if( other.name != null )
            return false;
    } else if( !name.equals( other.name ) )
        return false;
    return true;
    }
     }

    ...
    ...
    Person john1 = new Person("John");
    Person john2 = new Person("John");
    System.out.println("john1 == john2:" + (john1 == john2));
    System.out.println("john1.equals(john2):" + john1.equals(john2));

As you can see, "==" will return false (the objects are two different instances of Person), whereas equals will return true (because we defined that 2 Persons are equal when they have the same name)

查看更多
登录 后发表回答