Java.lang.String.equals() Method
Advertisements
Description
The java.lang.String.equals() method compares this string to the specified object. The result is true if and only if the argument is not null and is a String object that represents the same sequence of characters as this object.
Declaration
Following is the declaration for java.lang.String.equals() method
public boolean equals(Object anObject)
Parameters
anObject -- This is the object to compare this String against.
Return Value
This method returns true if the given object represents a String equivalent to this string, else false.
Exception
NA
Example
The following example shows the usage of java.lang.String.equals() method.
package com.tutorialspoint;
import java.lang.*;
public class StringDemo {
public static void main(String[] args) {
String str1 = "sachin tendulkar";
String str2 = "amrood admin";
String str3 = "amrood admin";
// checking for equality
boolean retval1 = str2.equals(str1);
boolean retval2 = str2.equals(str3);
// prints the return value
System.out.println("str2 is equal to str1 = " + retval1);
System.out.println("str2 is equal to str3 = " + retval2);
}
}
Let us compile and run the above program, this will produce the following result:
str2 is equal to str1 = false str2 is equal to str3 = true