Sign up ×  I ran into an interesting (and very frustrating)
issue with the equals() method today which caused what I thought
to be a well tested class to crash and cause a bug that took me
a very long time to track down.  Just for completeness, I wasn't
using an IDE or debugger - just good old fashioned text editor
and System.out's. Time was very limited and it was a school
project.  Anyhow -  I was developing a basic shopping cart which
could contain an ArrayList of Book objects. In order to
implement the addBook(), removeBook(), and hasBook() methods of
the Cart, I wanted to check if the Book already existed in the
cart. So off I go -  public boolean equals(Book b) {     ... //
More code here - null checks     if (b.getID() == this.getID())
return true;     else return false;  All works fine in testing.
I create 6 objects and fill them with data. Do many adds,
removes, has() operations on the Cart and everything works fine.
I read that you can either have equals(TYPE var) or
equals(Object o) { (CAST) var }