In this post, we will explain the difference between the == operator and the equals() method in Java, and provide some examples on how to use them correctly. We will also cover some common pitfalls and mistakes that we should avoid when comparing objects in Java.

Java also has some nuances and subtleties that can confuse beginners. One of these is the difference between the == operator and the equals() method, which are both used for comparion in Java.

1. Overview of the == operator

The == operator is a binary operator that compares two operands for equality. It can be used with both primitive types and reference types in Java. For primitive types, such as int, char, boolean, etc., the == operator compares the values of the operands. For example:

Download  Run Code

 
In this example, the == operator compares the values of x and y, which are 10 and 20 respectively, and returns false. For reference types, such as String, Object, etc., the == operator compares the references or memory locations of the operands. For example:

Download  Run Code

 
In this example, the == operator compares the references of s1, s2, and s3, which are objects of type String. The references of s1 and s2 point to the same string literals in the string constant pool, which is a special area of memory where Java stores literal strings. Therefore, s1 == s2 returns true. However, the reference of s3 points to a different object in the heap memory, which is created by using the new keyword. Therefore, s1 == s3 returns false.

2. Overview of the equals() method

The equals() method is defined in the Object class, which is the superclass of all classes in Java. It can be used to compare two objects for equality based on their content or state. The default implementation of the equals() method in the Object class is equivalent to using the == operator. For example:

Download  Run Code

 
In this example, the equals() method compares the references of o1 and o2, which are objects of type Object. Since they point to different objects in the heap memory, o1.equals(o2) returns false.

 
The equals() method behaves similarly to the == operator if not overridden by a class. Every class should override the equals() method of the Object class and provide a more meaningful comparison based on their attributes or fields. For example, the String class overrides the equals() method of the Object class to compare two strings based on their characters. For example:

Download  Run Code

 
In this example, the equals() method compares the contents of the backed char array of s1, s2, and s3, which are objects of type String. Since they have the same characters, s1.equals(s2) and s1.equals(s3) both return true. Since String is an object in Java, we should always use the equals() method for comparing two strings in Java. Let’s consider another example:

Download  Run Code

 
Here, comparing two strings using the == and equals() method returns the different output. This is because == is performing a reference comparison. Since we’re creating two new string objects using the String constructor, they will not share the same reference, and s1 == s2 returns false. Hence, we should never use the == operator for comparing two strings.

 
Also See:

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

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