Java equals Method

The Java equals method is a String Method, which compares a string with user-given Object data to check whether they both represent the same characters sequence or not. Based on the result, the Java string equals method will return Boolean True or False.

This article will show how to write string equals with an example. The basic syntax of the string equals method in Java Programming language is shown below.

Java equals Method Syntax

The following equals method will accept the Object as the argument and compare the string with this Object to check whether the string is identical to this Object or not.

public boolean equals(Object Obj); // It will return boolean True or False 

//In order to use in program
String_Object.equals(Object Obj)

Java equals Method Example

In this program, we use the Java equals method to check whether the string and user-specified object data represent the same sequence of characters. Within this example, we declared two String variables, str1, str2, and three Objects, str3, str4, and str5.

The first statement will call the public boolean equals (Object Obj) method to compare the str1 with str2. From the below screenshot, you can observe that it is returning True because String is an Object.

Next, we used the java equals method to compare Object str3 with another string object str4. The following statement will compare str1 with the object str5.

Next, we checked for non-equivalent values. Here “Tutorial Gateway” is not the same as the “Java Program”.

package StringFunctions;

public class EqualsMethod {
	public static void main(String[] args) {
		String str1 = "Java Programming";
		String str2 = "Java Programming";
		String str3 = new String("Tutorial Gateway");
		String str4 = new String("Tutorial Gateway");
		String str5 = new String("Java Programming");

		boolean a = str1.equals(str2);
		boolean b = str3.equals(str4);
		boolean c = str1.equals(str5);
		boolean d = str3.equals(str5);
		boolean e = str1.equals(str3);
		System.out.println("Does the String Object str1 Equals to str2? = " + a);
		System.out.println("Does the String Object str3 Equals to str4? = " + b);
		System.out.println("Does the String Object str1 Equals to str5? = " + c);
		System.out.println("Does the String Object str3 Equals to str5? = " + d);
		System.out.println("Does the String Object str1 Equals to str3? = " + e);
Java equals Method 1

equals Example

In this Java program, we will ask the user to enter any word.

The Java equals string method program will display the message based on the user-entered string value.

Within the Method example, we declared a String object strand assigned a corresponding value. The following statement will ask the user to enter any word. Next, we will assign the user-entered value to variable str.

Next, we used the If Else Statement to compare the user entered with the String Object “programs” and check whether they are identical.

  • If the statement inside the If is True, Java System.out.println(“Welcome to Programming Language”); statement will print
  • Else System.out.println(“Goodbye to Tutorials”); statement will print
package StringFunctions;

import java.util.Scanner;

public class EqualsMethodex {
	private static Scanner sc;
	public static void main(String[] args) {
		sc = new Scanner(;	
		String str = new String("programs");
		System.out.println("Please Enter any word: ");
		String str1 = sc.nextLine();
		if (str1.equals(str)) {
			System.out.println("Welcome to Java Programming Language");
		else {
			System.out.println("Goodbye to Tutorials");
Please Enter any word: 
Welcome to Programming Language

Let us enter a different word

Please Enter any word: 
Goodbye to Tutorials