Java string comparison equals – How to compare two Strings in Java using equals() Method? | String Comparison in Java using Equals Method

Java string comparison equals: A string is a Special Class in Java and the comparison of two strings is a common practice. There are several ways to compare two strings in Java. However, we have outlined the popular methods to compare two strings in Java.

  1. Using User Defined Function
  2. Using String.equals()
  3. Using String.equalsIgnoreCase()
  4. Using Objects.equals()
  5. Using String.compareTo()

This Tutorial on String Comparison using Equals() Method includes the following

Using String.equals() Definition and Usage

How to compare two string in java: We can use the equals() method to compare the content of the two strings. It returns true, if the content of the Two Strings is the same otherwise it returns false. The equals() method by default check the content including the case also i.e.The comparison is case sensitive. If the comparison is case insensitive it returns false.

Equals Method in Java

We can discuss the other way to compare the two strings in our next tutorials. Here, we can discuss only the equals() method.

String Comparison using Equals Method in Java

Syntax: The syntax of the equals() method of String class is given below:


str1 and str2 are the two strings to be compared.

Do Check:

Comparing Two Strings Using Equal Method in Java Example Program

Java string comparison.equals: The simple way to compare the two strings is by using the equals() method. By default, it checks the content including the case also. Let’s see the example.

class StringCompare{
public static void main(String args[]){
String str1 = "Javastudypoint";
String str2 = new String("Java");
String str3 = new String("Javastudypoint");
String str4 = "Java";
System.out.println(str1.equals(str3)); //true
System.out.println(str2.equals(str4)); //true
System.out.println(str1.equals(str2)); //false



How to compare two Strings in Java using equals() Method 1

Why not use == Operator for String Comparison?

Java compare two strings: Usually, both equals() and == Operator are used to compare two objects and check the equality. Below are some of the common differences between the two.

  • The Major Difference between the two is that one is a method and the other is an operator.
  • You can use the == Operators for reference comparison(address comparison) and .equals() method for content comparison.
  • In other words, we can say that == checks if both the objects point to the same memory location whereas .equals() method evaluates the comparison of values in objects.

Example Program to understand why not to use == Operator



Here two string objects are created using the name s1 and s2

  • s1 and s2 have two different objects.
  • When one uses == Operator for comparing strings s1 and s2 the result is false since it has different addresses in the memory.
  • using Equals() the result is true as it compares the values in both the strings s1 and s2.