-2

I´m having some trouble understanding this code. I know I need to create a method to compare non built-ins, but could someone please explain to me, in detail what the boolean equals() is actually doing?

public class Book {

     private String title;
     private Author author;
     private int year;
     public boolean equals(Object o) {
         if (o == this) { return true; }
         if (o == null) { return false;}
         if (!o.getClass().equals(Book.class)) {return false; }
         Book aBook = (Book) o;
         return aBook.year == year && aBook.title.equals(title) && aBook.author.equals(author);
     }
}

It was given to me by a friend, if you have any suggestions for improvements please tell me.

Mast
  • 1,788
  • 4
  • 29
  • 46
Pafmac289
  • 13
  • 3
  • 6
    @sᴜʀᴇsʜᴀᴛᴛᴀ No. This is [off-topic](http://codereview.stackexchange.com/help/on-topic) at Code Review on multiple levels. – Mast Aug 23 '15 at 17:18
  • @Mast: I'm not sure that it's on topic here. Regardless, this sort of thing can be found in most any Java textbook or any [basic Google search](https://www.google.com/?gws_rd=ssl#q=java+equals+method+implementation) (first couple of hits should be adequate). Not sure why this question is re-asking material that has been asked many times before on this and other sites. – Hovercraft Full Of Eels Aug 23 '15 at 17:31

1 Answers1

1

Check my comments in the your code:-

public boolean equals(Object o) {
         //below line is comparing if o refers to the same current object
         if (o == this) { return true; }
         //below line is checking if o is null, in that case it will return false
         if (o == null) { return false;}
         //checking if the class of o is Book, if not return false
         if (!o.getClass().equals(Book.class)) {return false; }
         //Casting the o from Object to Book class
         Book aBook = (Book) o;
         //comparing the book object's year. title and author, if all three are equal then only two Book objects are equal
         return aBook.year == year && aBook.title.equals(title) && aBook.author.equals(author);
     }

You will call the above method like below:-

Book object1=new Book();
Book object2=new Book();
object1.equals(object2);

Scenario 1:-First line in your code checks, whether object1 and object2 are reference to the same object:-

Book object1=new Book();
Book object2=object1;
object1.equals(object2);

Above situation is handled in your first line, you can see in scenario 1, object2 is basically the same current object that is object1.

Your null and class conditions can be written like:-

if((o == null) || (o.getClass() != this.getClass())) return false; //preferred

Don't get tempted to use:-

if(!(o instanceof Book)) return false; //avoid

because above condition fails to return false if the argument is a subclass of the class Book.

In the last part of your equals method, you are just comparing the three attributes of two Book objects and calling the two objects equal only if all the three attributes are equal.

Amit Bhati
  • 5,569
  • 1
  • 24
  • 45
  • 1
    I think Null Check is really not required , `o instanceof Book.class` will suffice! – Neeraj Jain Aug 23 '15 at 17:29
  • @NeerajJain Look at the code again. It's *not* doing an `instanceof`. – Andreas Aug 23 '15 at 17:35
  • @Andreas, it was just a suggestion, haven't downvoted though... I thought it would be better to improve approved Solution. – Neeraj Jain Aug 23 '15 at 17:38
  • Minor consideration, but you're overusing parenthesis in the *preferred* code. Use: `if (o == null || o.getClass() != this.getClass()) return false;` – Andreas Aug 23 '15 at 17:46