Javatpoint Logo
Javatpoint Logo

Difference between comparing String using == and .equals() method in Java

When comparing strings in Java, it is important to understand the difference between the == operator and the .equals() method.

In Java, a string is an object, and comparing objects requires considering whether you want to compare their references (memory addresses) or their actual contents.

The == operator checks for reference equality. It compares the memory addresses of two string objects to determine if they refer to the same location in memory. If two string objects reference the same memory address, == will return true, indicating that the objects are equal.

The .equals() method in Java is used to compare the actual content of two string objects, focusing on the sequence of characters they contain, rather than considering other factors like references or memory addresses. It checks if the characters in both strings are the same. If the content of the strings is identical, .equals() will return true, indicating that the objects are equal.

  1. The distinction between the .equals() method and the == operator lies in their nature: one is a method while the other is an operator.
  2. We generally use the == operator for reference comparison, whereas the .equals() method is for content comparison.
  3. A class will automatically utilize the equals(Object o) method of the closest parent class that has overridden this method, even though it does not override the equals method.

Filename: StringComparisonExample.java

Output:

true
false
true
true

Equality operator(==)

When used to compare strings, the equality operator compares the references (memory addresses) of the string objects rather than their contents. It determines whether two string objects refer to the exact same memory location. It returns true if the references are identical, meaning they point to the same memory address, and false if they do not. It is primarily used to compare primitive types and object references.

Filename: EqualityOperatorExample.java

Output:

true
false
true

.equals() Method

The .equals() method in Java is defined in the Object class, which is the root class for all objects in Java. It compares the content (values) of two objects for equality. The primary purpose of the .equals() method is to determine if two objects are meaningfully equivalent based on their internal state.

Filename: EqualsMethodExample.java

Output:

true
true
false
false

Difference between comparing String using == and .equals() method Table

Comparison Aspect == Operator .equals() Method
Comparison Basis Checks if the memory addresses of the objects are equal. Compares the content (values) of the objects.
Usage Applicable to all data types. Specifically designed for comparing objects, including strings.
Comparison Result Returns true if the objects have the same address. Returns true if the objects have the same content.
String Content Comparison Not suitable for comparing string content. Suitable for comparing string content.
Object Content Comparison Compares the object references, not their content. Compares the content of the objects, considering their internal state.
Called as == is considered an operator in Java. Equals() is considered a method in Java.






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA