Java - String equals() Method



The Java String equals() method is used to check whether the current string is equal to specified object or not. It returns true if the string instances contain the same characters in the same order else, it returns false.

The equals() method accepts an object as a parameter which is comparable. It does not throw any exception while comparing or checking whether the string is equal to the specified object or not. This method returns a Boolean value.

Syntax

Following is the syntax of the Java String equals() method −

public boolean equals(Object anObject)

Parameters

  • anObject − This is an object to compare with this String.

Return Value

This method returns true if the given object represents a String equivalent to this string, else false.

Example

If the given string value is the same as the specified object value, the equals() method returns true.

In the following program, we are instating the String class with the value “Java”. Using the equals() method, we are trying to determine whether the given string is equal to the specified object “Java” or not.

import java.lang.*;
public class Equal {
   public static void main(String[] args) {
      
      //instantiate a String class
      String str = new String("Java");
      
      //initialize the string object
      String obj = "Java";
      System.out.println("The given string is: " + str);
      System.out.println("The object is: " + obj);
      
      //using the equals() method
      System.out.println("The given string is equal to the specified object or not? " + str.equals(obj));
   }
}

Output

On executing the above program, it will produce the following result −

The given string is: Java
The object is: Java
The given string is equal to the specified object or not? true

Example

If the given string value is the different from the specified object value, the equals() method returns false.

In the following example, we are creating a string literal with the value “HelloWorld”. Using the equals() method, we are trying to determine whether the given string is equal to the specified object “Hello” or not.

import java.lang.*;
public class Equal {
   public static void main(String[] args) {
      
      //create a String literal
      String str = "HelloWorld";
      
      //initialize the string object
      String obj = "Hello";
      System.out.println("The given string is: " + str);
      System.out.println("The object is: " + obj);
      
      //using the equals() method
      System.out.println("The given string is equal to the specified object or not? " + str.equals(obj));
   }
}

Output

Following is the output of the above program −

The given string is: HelloWorld
The object is: Hello
The given string is equal to the specified object or not? false

Example

Using a conditional statement to determine whether a given string is equal to the specified object.

In this program, we are creating a string with the value “TutorialsPoint”. Using the equals() method and conditional statement, we are trying to check whether the given string is equal to the specified object “Java” or not.

import java.lang.*;
public class Equal {
   public static void main(String[] args) {
      
      //create a String literal
      String str = "TutorialsPoint";
      
      //initialize the string object
      String obj = "Java";
      System.out.println("The given string is: " + str);
      System.out.println("The object is: " + obj);
      
      //using the equals() method
      boolean bool = str.equals(obj);
      if(bool) {
         System.out.println("The given string is equal to the specified object.");
      } else {
         System.out.println("The given string is not equal to the specified object.");
      }
   }
}

Output

The above program, produces the following output −

The given string is: TutorialsPoint
The object is: Java
The given string is not equal to the specified object.

Example

If the given string value is same as the object value, but the cases are different, this method return false.

In this program, we are creating a string literal with the value “TutorialsPoint”. Using the equals() method, we are trying to check whether the given string is equal to the specified object “tutorialspoint” or not. Since the method is case-sensitive, this method returns false, even if the values are the same.

import java.lang.*;
public class Equal {
   public static void main(String[] args) {
      
      //create a String literal
      String str = "TutorialsPoint";
      
      //initialize the string object
      String obj = "tutorialspoint";
      System.out.println("The given string is: " + str);
      System.out.println("The object is: " + obj);
      
      //using the equals() method
      boolean bool = str.equals(obj);
      System.out.println("The equals() method returns " + bool);
      if(bool) {
         System.out.println("The given string is equal to the specified object.");
      } else {
         System.out.println("The given string is not equal to the specified object.");
      }
   }
}

Output

After executing the above program, it generates the following output −

The given string is: TutorialsPoint
The object is: tutorialspoint
The equals() method returns false
The given string is not equal to the specified object.
java_lang_string.htm
Advertisements