- Java.lang Package classes
- Java.lang - Home
- Java.lang - Boolean
- Java.lang - Byte
- Java.lang - Character
- Java.lang - Character.Subset
- Java.lang - Character.UnicodeBlock
- Java.lang - Class
- Java.lang - ClassLoader
- Java.lang - Compiler
- Java.lang - Double
- Java.lang - Enum
- Java.lang - Float
- Java.lang - InheritableThreadLocal
- Java.lang - Integer
- Java.lang - Long
- Java.lang - Math
- Java.lang - Number
- Java.lang - Object
- Java.lang - Package
- Java.lang - Process
- Java.lang - ProcessBuilder
- Java.lang - Runtime
- Java.lang - RuntimePermission
- Java.lang - SecurityManager
- Java.lang - Short
- Java.lang - StackTraceElement
- Java.lang - StrictMath
- Java.lang - String
- Java.lang - StringBuffer
- Java.lang - StringBuilder
- Java.lang - System
- Java.lang - Thread
- Java.lang - ThreadGroup
- Java.lang - ThreadLocal
- Java.lang - Throwable
- Java.lang - Void
- Java.lang Package extras
- Java.lang - Interfaces
- Java.lang - Errors
- Java.lang - Exceptions
- Java.lang Package Useful Resources
- Java.lang - Useful Resources
- Java.lang - Discussion
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.