Difference between comparing String using == and .equals() method in Java
In Java, there is a difference between comparing strings using the == operator and the .equals() method.
We have to use the "==" operator to compare strings only when you want to check if two string variables refer to the exact same memory location. If you want to compare the contents of two strings, you should use the .equals() method to ensure accurate comparison.
Sr. NoAttribute“==”“.equals() Method”1TypeIt’s a Java Binary OperatorIt’s a Java Method of Object Class2Compare LogicIt Compares variable and object references – memory location in the Java heap.It Compares the state or content of the object- as per the logic in equals method()3Override BehaviourBehaviour can not be overridden.equals() method can be overridden as it’s a Java method4String Object ComparisonTrue, if objects are referring to same addressTrue, if objects holds the same string value5ObjectsTrue, If Object points to the same memory locationPreferred to choose equals() method. ability to change compare logic for equality6Objects created by new KeywordAlways return false though contents are the same.True if contents are same false if contents are not sameThe == operator
The == operator compares the object references of two strings, not their actual contents. It checks if the two string references point to the same memory location. In other words, it checks if the two string objects are exactly the same object in memory.
For example:
FileName: StringComparisonExample.java
Here the given Java program demonstrates the use of the == operator for string comparison, Integer Comparison and Object Comparison.
public class StringComparisonExample { public static void main(String[] args) { // String comparison String str1 = "Hello"; String str2 = "Hello"; String str3 = new String("Hello"); System.out.println("Comparing using == operator:"); System.out.println("str1 == str2: " + (str1 == str2)); // Comparing string literals System.out.println("str1 == str3: " + (str1 == str3)); // Comparing string literal with a new string object // Integer comparison int num1 = 10; int num2 = 10; System.out.println(" Comparing using == operator:"); System.out.println("num1 == num2: " + (num1 == num2)); // Comparing integer variables // Object comparison Integer obj1 = new Integer(5); Integer obj2 = new Integer(5); System.out.println(" Comparing Integer objects using == operator:"); System.out.println("obj1 == obj2: " + (obj1 == obj2)); // Comparing Integer objects using the == operator } }
Output
Comparing using == operator:str1 == str2: true str1 == str3: false Comparing using == operator: num1 == num2: true Comparing Integer objects using == operator: obj1 == obj2: false
The .equals() Method
In Java, the .equals() method compares the actual content of two strings. It checks if the characters inside the strings are the same.
Example 2
In this java program, we have two scenarios for using the .equals() method:
- String comparison: We have three strings, str1, str2, and str3. We use the .equals() method to compare str1 with str2 and str1 with str3.
- Object comparison: We create two Integer objects, obj1 and obj2, both with a value of 5. We use the .equals() method to compare them.
FileName: StringEqualsExample.java
public class StringEqualsExample { public static void main(String[] args) { // String comparison String str1 = "Hello"; String str2 = "Hello"; String str3 = new String("Hello"); System.out.println("Comparing using .equals() method:"); System.out.println("str1.equals(str2): " + str1.equals(str2)); System.out.println("str1.equals(str3): " + str1.equals(str3)); // Object comparison Integer obj1 = new Integer(5); Integer obj2 = new Integer(5); System.out.println(" Comparing Integer objects using .equals() method:"); System.out.println("obj1.equals(obj2): " + obj1.equals(obj2)); } }
Output
Comparing using .equals() method:str1.equals(str2): true str1.equals(str3): true Comparing Integer objects using .equals() method: obj1.equals(obj2): true
Example 3
In the given java program as you can see, the == operator correctly compares the references of the string objects, while the .equals() method compares the actual string contents to determine equality.
FileaName: ComparisonExample.java
public class ComparisonExample { public static void main(String[] args) { String str1 = "Hello"; String str2 = "Hello"; String str3 = new String("Hello"); // String comparison using == operator System.out.println("String comparison using the == operator:"); System.out.println(str1 == str2); // true System.out.println(str1 == str3); // false // String comparison using .equals() method System.out.println(" String comparison using the .equals() method:"); System.out.println(str1.equals(str2)); // true System.out.println(str1.equals(str3)); // true // Object comparison using == operator System.out.println(" Object comparison using the == operator:"); Object obj1 = new Object(); Object obj2 = new Object(); Object obj3 = obj1; System.out.println(obj1 == obj2); // false System.out.println(obj1 == obj3); // true // Object comparison using .equals() method System.out.println(" Object comparison using the .equals() method:"); System.out.println(obj1.equals(obj2)); // false System.out.println(obj1.equals(obj3)); // true } }
Output
String comparison using the == operator: true false String comparison using the .equals() method: true true Object comparison using the == operator: false true Object comparison using the .equals() method: false true