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

In this part of Java programming mistakes, we will take a look on 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 physical and logical sense, and in case of domain objects it's mostly driven by business rules, comparing objects with "==" operator, introduces subtle bugs, which are hard to find. Difference between equals() and == operator,  one of the Java classics is also asked to find out if 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 involve in comparing two Enum, which you learn by following that link. One of the most common pattern of this mistake is is comparing two Strings with == operator, which we will see in this article. By the way this is third in series of common Java programming mistakes, and if you haven't read the previous two, you can read them here :
Read more »

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

Posting Komentar