In this post, we will learn the difference between “==” Operator and equals() in Java. This is a frequently asked question in Java interviews for beginners. Let's dive into it.
Difference between “==” Operator and equals() Method in Java
Features | “==” Operator | equals() Method |
---|---|---|
Type | It is a binary operator in Java. | It is a public method of java.lang.Object class. |
Comparison Base | It compares the two objects based on their location in the memory. | The default version of the equals method also does the comparison of two objects based on their location in the memory. But, you can override the equals method so that it performs the comparison of two objects on some condition. |
Usage | It can be used on both primitive types as well as on derived types. | It can be used only on derived types. |
Best Suitable For | It is best suitable for primitive types. | It is best suitable for derived types. |
Override | You can’t override the “==” operator. It behaves the same for all objects. | You can override the equals method according to your business requirement |
Example
Let's create an example to illustrate the difference between the == operator and the equals() method in Java.
In Java, the == operator is used to compare the references of objects, i.e., it checks if two variables point to the same memory location. On the other hand, the equals() method is used to compare the content or values of objects, and it is usually overridden by classes to provide custom comparison logic.
Example using == operator:
public class EqualsOperatorExample {
public static void main(String[] args) {
String str1 = "Hello";
String str2 = "Hello";
String str3 = new String("Hello");
System.out.println("Using == operator:");
System.out.println("str1 == str2: " + (str1 == str2)); // true (both point to the same "Hello" literal)
System.out.println("str1 == str3: " + (str1 == str3)); // false (str3 points to a new object)
Integer num1 = 42;
Integer num2 = 42;
Integer num3 = new Integer(42);
System.out.println("\nUsing == operator with Integers:");
System.out.println("num1 == num2: " + (num1 == num2)); // true (both point to the same Integer cache)
System.out.println("num1 == num3: " + (num1 == num3)); // false (num3 points to a new Integer object)
}
}
Output:
Using == operator:
str1 == str2: true
str1 == str3: false
Using == operator with Integers:
num1 == num2: true
num1 == num3: false
In the == operator example, we have two String objects str1 and str2, both initialized with the same value "Hello". When we compare them using ==, it returns true because both str1 and str2 point to the same string literal in the string pool. However, when we compare str1 with a new String object str3, initialized using a new String("Hello"), the == comparison returns false since str3 points to a different memory location.
Example using equals() method:
public class EqualsMethodExample {
public static void main(String[] args) {
String str1 = "Hello";
String str2 = "Hello";
String str3 = new String("Hello");
System.out.println("Using equals() method:");
System.out.println("str1.equals(str2): " + str1.equals(str2)); // true (both have the same content)
System.out.println("str1.equals(str3): " + str1.equals(str3)); // true (both have the same content)
Integer num1 = 42;
Integer num2 = 42;
Integer num3 = new Integer(42);
System.out.println("\nUsing equals() method with Integers:");
System.out.println("num1.equals(num2): " + num1.equals(num2)); // true (both have the same value)
System.out.println("num1.equals(num3): " + num1.equals(num3)); // true (both have the same value)
}
}
Output:
Using equals() method:
str1.equals(str2): true
str1.equals(str3): true
Using equals() method with Integers:
num1.equals(num2): true
num1.equals(num3): true
In the equals() method example, we again have two String objects str1 and str2, both initialized with the same value "Hello". When we compare them using the equals() method, it returns true because the equals() method compares the content, and both strings have the same content.
Similarly, when we compare str1 with a new String object str3 using the equals() method, it also returns true because, again, the equals() method compares the content, and both strings have the same content.
The same applies to the Integer objects num1, num2, and num3. When we use the equals() method to compare them, it returns true because the equals() method compares the values, and all three Integer objects have the same value 42.
Conclusion
In conclusion, the == operator checks for reference equality, whereas the equals() method checks for content (value) equality. It's essential to choose the appropriate one depending on the comparison you want to perform. For objects like String and Integer, where content comparison is required, using the equals() method is generally recommended.
Collection Framework
Interview Questions
Java
X vs Y
Comments
Post a Comment