In Java, there is a equals() method which is used to compare strings. This comparison of strings are based on the content of the string.

How the equals() method works in Java?

equals() methods compare two strings and if all characters of a string is not matched then it will return false. And If all the characters of the string matched it will return true.

equals() method of String class overrides the equals() method of Object class.

Below is the internal implementation of the equals() method.

public boolean equals(Object anObject) {
if (this == anObject) {
return true;
if (anObject instanceof String) {
String anotherString = (String) anObject;
int n = value.length;
if (n == anotherString.value.length) {
char v1[] = value;
char v2[] = anotherString.value;
int i = 0;
while (n– != 0) {
if (v1[i] != v2[i])
return false;
return true;
return false;

Signature and Parameter of equals() method.

public boolean equals(Object anotherObject)

Return Type

boolean value true or false. if string is equal then return true otherwise return false.

Another Program to demonstrate equals method in Java

public class EqualsExample{
public static void main(String args[]){
String s1=”tutorialworld”;
String s2=”tutorialworld”;
String s3=”java”;



As you can see in the above example string s1 and s2 are the same as both have the same value “tutorialworld” but string s3 have different content “java”.

So if we compare s1 and s2 then it returned true and if we have compared s1 and s3 then it has returned false as it is not equal.

See Below one more example of List element comparision.

import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
String str1 = “Prakash”;
ArrayList name = new ArrayList<>();
for (String str : name) {
if (str.equals(str1)) {
System.out.println(“Prakash is available in list”);

Prakash is available in list