String equals() Method in Java - GeeksforGeeks

String equals() Method in Java

Last Updated : 21 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

The equals() method of the String class compares the content of two strings. It returns false if any of the characters are not matched. It returns true if all characters are matched in the Strings. It compares the value’s character by character, irrespective of whether two strings are stored in the same memory location. The String equals() method overrides the equals() method of the object class.

Syntax:

string.equals(object)

Return Type: The return type for the equals method is Boolean.

Examples of String equals() Method in Java

Below is the code implementation of the usage of the String equals() method in Java.

Example 1: Basic Comparision

Java




// Java program to demonstrate String equals() method
import java.io.*;
class Main 
{
  public static void main(String[] args) 
  {
    String str1 = "Learn Java";
    String str2 = "Learn Java";
    String str3 = "Learn Kotlin";
    boolean result;
  
    // comparing str1 with str2
    result = str1.equals(str2);
    System.out.println(result);   
  
    // comparing str1 with str3
    result = str1.equals(str3);
  
    System.out.println(result); 
  
    // comparing str3 with str1
    result = str3.equals(str1);
    System.out.println(result);  
  }
}


Output

true
false
false


Explanation of the above Program:

In the above program,

  • We created three strings str1, str2, and str3.
  • Next, we compared str1 with str2 and str3 with str3 using the equals() method.
  • Finally, it prints the result in Boolean format.

Example 2: Case-Insensitive Comparison

It compares the difference between uppercase and lowercase characters.

Java




import java.io.*;
  
public class CaseInsensitiveComparisonExample 
{
    public static void main(String[] args) 
    {
        // define three strings
        String str1 = "Hello";
        String str2 = "hello";
        String str3 = "WORLD";
  
  
        // comparison between str1 and str2
        System.out.println("str1.equals(str2): " + str1.equals(str2)); 
  
        // comparison between str1 and str2
        System.out.println("str1.equalsIgnoreCase(str2): " + str1.equalsIgnoreCase(str2)); 
  
        // comparison between str2 and str3
        System.out.println("str2.equalsIgnoreCase(str3): " + str2.equalsIgnoreCase(str3)); 
    }
}


Output

str1.equals(str2): false
str1.equalsIgnoreCase(str2): true
str2.equalsIgnoreCase(str3): false


Explanation of the above Program:

In the above program,

  • We compare three strings using the equalsIgnoreCase() method of str1, str2, and str3.
  • The results show that str1 and str2 are not equivalent in comparisons.
  • It also shows that str2 and str3 are not equivalent in case-insensitive comparisons.


Similar Reads

Collator equals(String, String) method in Java with Example
The equals() method of java.text.Collator class is used to check if both strings are identical or not. Syntax: public boolean equals(String source, String target) Parameter: This method takes two strings between which comparison is going to take place. Return Value: if both strings are equal to each other then it will return true otherwise false. B
2 min read
Difference between comparing String using == and .equals() method in Java
Both the equals() method and the == operator are used to compare two objects in Java. The Java string equals() method, compares two strings and returns true if all characters match in both strings, else returns false. The == operator compares the reference or memory location of objects in a heap, whether they point to the same location or not.Whene
6 min read
Method Class | equals() Method in Java
The java.lang.reflect.Method.equals(Object obj) method of Method class compares this Method Object against the specified object as parameter to equal(object obj) method. This method returns true if the Method object is the same as the passed object. Two Methods are the same if they were declared by the same class and have the same name and formal p
3 min read
equals() on String and StringBuffer objects in Java
Consider the following codes in java: Java Code // This program prints false class GFG { public static void main(String[] args) { StringBuffer sb1 = new StringBuffer("GFG"); StringBuffer sb2 = new StringBuffer("GFG"); System.out.println(sb1.equals(sb2)); } } Output: false Java Code // This program prints true class GFG { public
2 min read
Java.util.Arrays.equals() in Java with Examples
Today we are going to discuss the simplest way to check whether two arrays are equal or not. Two arrays are considered equal if both arrays contain the same number of elements, and all corresponding pairs of elements in the two arrays are equal. In other words, two arrays are equal if they contain the same elements in the same order. Also, two arra
4 min read
Calendar.equals() method in Java
java.util.Calendar.equals() is a method in Calendar class of java.util package. The method compares this Calendar to the specified Object.The method returns true if this object is equal to object. If this is not the case, i.e, if there is any difference in the parameters between the two Calendars, then false is returned. Syntax : public boolean equ
2 min read
Java Long equals() method with Examples
The java.lang.Long.equals() is a built-in function in java that compares this object to the specified object. The result is true if and only if the argument is not null and is a Long object that contains the same long value as this object. It returns false if both the objects are not same. In all other cases, compareTo method should be preferred. S
3 min read
Character.equals() method in Java with examples
The java.lang.Character.equals() is a function in Java which compares this object against the specified object. If the argument is not null then the result is true and is a Character object that represents the same char value as this object. Syntax: public boolean equals(Object obj) Parameters: The function accepts a single parameter obj which spec
2 min read
Double.equals() Method in Java with Examples
The java.lang.Double.equals() is a built-in function in java that compares this object to the specified object. The result is true if and only if the argument is not null and is a Double object that contains the same double value as this object. It returns false if both the objects are not same. In all other cases, compareTo method should be prefer
2 min read
MathContext equals() Method in Java
The java.math.MathContext.equals() is an in-built function in Java which checks for equality between this MathContext object with the object passed as parameter to the function. The function returns true if the context settings of both the aforementioned objects are same. Syntax : public boolean equals(Object obj) Parameters : The function accepts
2 min read
Practice Tags :