5

This is my implementation of the equals class for a Coor class which is just contains 2 ints x and y. would this be the proper way of implementing this method?

 public boolean equals(Object obj) {
        if (obj == null || obj.getClass() != this.getClass()) {
            return false;
        }
        Coor temp = (Coor) obj;
        if (temp.x == this.x && temp.y == this.y) {
            return true;
        } else {
            return false;
        }
    }
user979490
  • 261
  • 3
  • 10
  • 2
    Yes. If all the fields are the same, and this is what you mean by "equals", then you've made the method correctly. This won't work if you're comparing something that inherits from the current (`this`) class, but it will work if they are. –  Oct 05 '11 at 00:13
  • 1
    don't forget to override the hashcode function should you wish to use it in Collections (even if behind the screens) – ratchet freak Oct 05 '11 at 00:26

7 Answers7

7

You could add one more check for reflexive equality (equal to self):

 public boolean equals(Object obj) {

    // Reflexive equality: did I get passed myself?
    if(this == obj){
        return true;
    }

    if (obj == null || obj.getClass() != this.getClass()) {
        return false;
    }

    Coor temp = (Coor) obj;
    return temp.x == this.x && temp.y == this.y;
}
Pat
  • 23,890
  • 6
  • 66
  • 67
  • could you explain what the "this==obj" is actually comparing?, is it just checking if it is the same object? – user979490 Oct 05 '11 at 00:23
  • 1
    Memory location of the objects. – Bhesh Gurung Oct 05 '11 at 00:24
  • 1
    It's checking the 2 object references (`this` and `obj`) to see if they point to the same memory location. If they do, they are the same object. – Pat Oct 05 '11 at 00:31
  • Although it doesn't hurt, this does add weight to your code with--well probably absolutely no benefit at all after the system is through optimizing it (Google premature optimization for reasons). – Bill K Oct 05 '11 at 21:26
5

Yes, it would.

Also be sure to override your hashCode() method--never override one without doing the other as well, it will confuse the hell out of your collections.

Your case could use a hash where it simply shifts one of the ints 32 bits and adds it to the other creating a completely unique long (a perfect hash function in this case--no collisions)

Bill K
  • 60,031
  • 14
  • 96
  • 147
  • i'm going to have to read up on that becuase i have no idea what haseCode() is or what it does, but thanks for the suggestion – user979490 Oct 05 '11 at 00:25
3

Seems ok. For brevity sake, you can do:

return temp.x == this.x && temp.y == this.y

Instead of

if (temp.x == this.x && temp.y == this.y) {
        return true;
    } else {
        return false;
    }

Also, please keep in mind the Object Contract (seriously!).

See the accepted answer here: What issues should be considered when overriding equals and hashCode in Java?

This can save you a huge about of headache in the future.

Community
  • 1
  • 1
pcalcao
  • 15,237
  • 1
  • 40
  • 62
1

Check this out:

http://www.javapractices.com/topic/TopicAction.do?Id=17

If that article is too much detail, then the short of it is: Your implementation is correct, but you should keep some other things in mind:

  1. You will also have to implement hashCode.

  2. equals will no longer commpare the object's identity. Doesn't sound like that's a problem for you.

  3. You could add the @Override annotation to your equals method.

user470714
  • 2,788
  • 26
  • 33
0

Here’s a more straightforward way:

public boolean equals(Object other) {
    return other instanceof Coor
      && ((Coor) other).x == x
      && ((Coor) other).y == y
}
Daniel Brockman
  • 16,714
  • 3
  • 25
  • 40
0

I believe this would work, at a quick glance. I say this because:

  1. It handles a null/improper types well.
  2. Performing x.equals(y) would yield the same result as y.equals(x).
  3. Performing x.equals(x) would return true.
  4. Performing x.equals(y) == true and y.equals(z) == true implies that x.equals(z) == true

This question has certainly been asked many times before though. See here: Overriding equals and hashCode in Java. A book called Effective Java discusses this topic in great detail too, and the particular chapter is linked off of there.

Community
  • 1
  • 1
pseudoramble
  • 2,371
  • 19
  • 28
0

There's only one source to read for how to override equals and hashCode: chapter 3 of Joshua Bloch's "Effective Java".

If you have a good IDE, like IntelliJ, it'll generate equals and hashCode the right way for you.

duffymo
  • 293,097
  • 41
  • 348
  • 541