Java String equals() method Example

Java String equals() method Example

The Java String equals() method tests the two strings based on content of the string.This method Returns true if the two Strings are same otherwise it will returns false.

method signature


public boolean equals(Object anObject)

Example


package com.practice;

public class StringequalsDemo {


public static void main(String[] args) {
// TODO Auto-generated method stub
String string1 = "apple";
String string2 = "banana";
String string3 = "apple";
String string4 = "animal";
System.out.println(string1.equals(string3));//returns true because here string1 and string2 are same.
System.out.println(string2.equals(string4));//returns false becuase both strings are different


}

}

output


true
false

-->