Just a small post to tell you an other story of my daily learning of Java programming.
Always use .equals() method instead of ==
In our application, I was always comparing objects using ==. All worked fine until …. we decided to use a database to persist our objects across application launches and to avoid OutOfMemoryError. Why ???
The answer is easy, each time an object is retrieved from the database, we create a NEW Java object initialized with the values stored in the database. Thus, the == operator is no more valid between to different Java objects representing the same “conceptual object”.
After overriding the equals and hashCode methos for my persitent objects I had to refactor all my lines of code to replace all == by equals method ;o)
Posted by Manuel
Posted by Manuel