A few months ago I had a short discussion in a merge request with an experienced developer who asked me
that’s why
asked to undo my refactoring because == was easier to read.
What happened?
In true clean code fashion, I left code better than I found it. I’m about one
place in the code
stumbled upon comparing objects, more specifically Boolean with ==. Of course I corrected that
a comparison with
equlas(). I was then asked to change this back to == so that it would be easier to read.
Of course I rejected that and provided the following explanation.
Why .equals() and not == for object comparison in Java?
Hello everyone,
Today we’re talking about an important difference in Java that often leads to errors: comparing objects. During the
The == operator works fine with primitive data types like int or boolean, but we should always do so with objects
use the .equals() method. But why is that? And what happens if we don’t?
== vs. .equals() – A fundamental difference
The == operator compares references in Java. This means it checks whether two variables refer to the same object in the
Show memory**. This is not a problem with primitive data types because they store their value directly. For objects
Variables, on the other hand, only store references to the actual objects.
The .equals() method, on the other hand, also compares references (as defined in the Object class) by default. Many
However, classes such as String, Integer or Boolean override this method to *
compare content*. So it checks whether two objects have the same value, even if they
occupy different memory addresses.
The problem is demonstrated using Boolean as an example
Let’s consider the following example with Boolean objects:
Boolean a = new Boolean(true);
Boolean b = new Boolean(true);
Boolean c = a;
System.out.println(a == b); // Output: false
System.out.println(a.equals(b)); // Output: true
System.out.println(a == c); // Output: true
What’s happening here?
a == b:aandbare two differentBooleanobjects, even if they have the same value (true).==compares the references and finds that they are different.a.equals(b): Theequals()method of theBooleanclass compares the values of the objects. Since both aretrue,.equals()returnstrue.a == c:cwas set to the same reference asa. Therefore both variables point to the same object in memory, and==returnstrue.
Why .equals() is the better choice
In most cases, we want to know whether two objects have the same content value, not whether they are random
are at the same memory address. The .equals() method gives us this possibility.
Best Practice: When comparing objects, always use the .equals() method (if it makes sense
was overwritten). The == operator should only be used on objects in very special cases, such as when you
want to explicitly check whether two variables point to the same object (e.g. in certain performance-critical
situations or when implementing equals() itself).
When do I need to override equals()?
If you create your own classes, they will inherit the default implementation of equals() from the Object class. This one
Default implementation only compares references. So if you want objects of your class based on their attributes
To be compared, you must override the equals() method.
Remember that when you override equals() you should also override the hashCode() method to get the
To ensure consistency with hash-based data structures (like HashMap or HashSet).
Conclusion
Comparing objects in Java can be tricky. With this knowledge you are well prepared to… Avoid pitfalls and write correct, robust code.
Do you have any questions or comments? Let me know!
![[EN] Warum `.equals()` und nicht `==` für Objektvergleiche in Java?](/images/BlogHeaderEquals.jpeg)