In this post, we will see how to use the == operator and equals() method correctly in Java.

1. Overview of == operator and equals() method

The == is a relational operator in Java that is used to compare primitive types such as int, double, char, float, etc. We should not use it to compare two object references since for any non-null reference values x and y, x == y returns true only when x and y refer to the same object.

 
The equals() method in the Object class is used to compare objects. It behaves similarly to the == operator if not overridden by a class. Every class should override the equals() method of the Object class and specify the equivalence relation on objects. The equivalence relation should be such that equals() methods evaluate the comparison of values in the objects irrespective of the two objects refer to the same instance or not. Since equal objects must have equal hash codes, it is necessary to override the hashCode() method.

2. How to use the == operator and equals() method correctly in Java?

The general rule of thumb is to use the == operator when we want to compare primitive types or references of objects, and use the equals() method when we want to compare the contents or states of objects. However, there are some exceptions and caveats that we should be aware of when using these operators and methods. Here are some tips and best practices that we should follow:

1. Using with complex expressions

Always use parentheses when using the == operator and equals() method with complex expressions or logical operators. This will avoid any confusion or ambiguity about the order of evaluation or precedence. For example:

2. Using with Strings

Always use equals() method when comparing strings or comparing other objects that override this method. Do not use == operator for comparing strings or other objects that override this method unless we are sure that we want to compare their references. This will avoid any unexpected or incorrect results due to different memory locations or implementations. For example:

3. Using with possible null objects

Always check for null values before using the equals() method. If we call the equals() method on a null reference, we will get a NullPointerException, which is a runtime exception that can crash our program. To avoid this, we should always check if the object is null before calling the equals() method. For example:

4. Using with Objects

Always override the equals() method and the hashCode() method together when creating our own classes. The hashCode() method is another method that is defined in the Object class, and it returns an integer value that represents the hash code of an object. The hash code is used by some data structures and algorithms, such as hash tables and sets, to store and retrieve objects efficiently. The contract between the equals() method and the hashCode() method is that if two objects are equal according to the equals() method, they must have the same hash code according to the hashCode() method. Therefore, if we override the equals() method in our class, we should also override the hashCode() method to ensure consistency and correctness.

Download  Run Code

That’s all about using the == operator and equals() method correctly in Java.