简体   繁体   中英

Java Newbie learning about polymorphism

This equals() method will produce the same output if lines 3-5 are omitted (I numbered these lines). What is the point of these lines?

/** Return true if that Beetle has the same parts as this one. */   
public boolean equals(Object that) {  
    3. if (this == that) {  
    4.    return true;  
    5. }  
       if (that == null) {  
         return false;  
       }  
      if (getClass() != that.getClass()) {  
         return false;  
      }

      Beetle thatBeetle  = (Beetle) that;  

      return body == thatBeetle.body   
            && eyes == thatBeetle.eyes   
            && feelers == thatBeetle.feelers   
            && head == thatBeetle.head   
            && legs == thatBeetle.legs   
            && tail == thatBeetle.tail;   
}

Checking == reference equality is fast, and if true the object is being compared to itself -- so is by definition equal.

This is often used a first step when comparing objects, as it's much faster than comparing all the details. But it's more commonly used from the caller/client function, rather than inside the equals() implementation.

For example, in a linear search:

public int indexOfBeetle (Beetle beetle, List<Beetle> list) {
    for (int i = 0; i < list.size(); i++) {
        Beetle cand = list.get( i);
        if (cand == beetle || cand.equals( beetle))
            return i;    // Found.
    }
    // Not Found.
    return -1;
}

The operator == checks if the objects are the same instance in memory whereas when you override equals you usually want to perform a logical test.

Let's take an example:

public class Person {
   private String name;

   // Here there are constructor and getters and setters
}

Now let's run these lines:

Person a = new Person();
Person b = a;
Person c = new Person();

If you compare these instances with == this is what you'll get:

a == a ==> true
a == b ==> true
a == c ==> false

Now, let's set the name:

a.setName("Joe"); // This also sets b because they're the same object
c.setName("Joe");

If our equals looks like this:

public boolean equals (Object other) {
    if(other == this) return true;

    if(other instanceof Person == false) return false;


    if(this.getName().equals(((Person) other).getName())) return true;

}

So we'll now get that a.equals(c) is true even though a==c is false.

So, why do we have the first line? - Some objects' equality is more expensive to compute and by checking this condition at the beginning you might spare some unnecessary computations

Doing that are you checking if the two objects are pointing to the same memory address. Using .equals without it you will achieve the same result because if they are pointing to the same memory address, it's obviously they are equals. But doing that it's much more faster, that's why most developers put this lines on .equals

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM