Saturday 21 May 2016

Java Mistake 3 - Using "==" instead of equals() to compare Objects in Java

In this part of Java programming mistakes, we will take a look at another common pattern, where programmers tend to use "==" operator to compare Objects, similar to comparing primitives. Since equality of object can be very different in the physical and logical sense, and in the case of domain objects it's mostly driven by business rules, comparing objects with "==" operator, introduces subtle bugs, which are hard to find. The difference between equals() and == operator,  one of the Java classics is also asked to find out if the developer is familiar with this important concept or not. Using == operator only make sense when comparing primitives like int, or final constants like Enum. Though there is more involved in comparing two Enum, which you learn by following that link.
Read more »

1 comment:

  1. Nice article. Thanks for sharing.
    https://www.flowerbrackets.com/difference-between-equal-operator-vs-equals-method-java/

    ReplyDelete