Java equals Method

The Java equals method is a Java 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.

In this article, we will show how to write string equals in Java Programming language with example. The basic syntax of the string.equals in Java Programming language is as shown below.

Java equals Method syntax

The following Java equals method will accept the Object as the argument and compare the string with this Object to check whether the string is equal 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 Example

In this Java program, we use the Java string equals method to check whether the string and user-specified object data represents the same sequence of characters or not.

// Java String.equals example
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

Within this Java string equals Method example, we declared two String variables str1, str2, and assigned corresponding values using the following statement.

String str1 = "Java Programming";
String str2 = "Java Programming";

Next, we declared three String Objects str3, str4, str5, and assigned corresponding values using the following statement.

String str3 = new String("Tutorial Gateway");
String str4 = new String("Tutorial Gateway");
String str5 = new String("Java Programming");

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

boolean a = str1.equals(str2);

This Java equals statement will compare the string Object str3 with another string object str4.

boolean b = str3.equals(str4);

The following Java string equals statement will compare the string variable str1 with the string object str5.

boolean c = str1.equals(str5);

Next, we checked for non-equivalent values. Here “Tutorial Gateway” is not at all equals to “Java Program”.

boolean d = str3.equals(str5);
boolean e = str1.equals(str3);

Lastly, we used the System.out.println statements will print the output

Java equals Method Example

In this Java program, We are going to ask the user to enter any word. Based on the user entered string value, the equal string program will display the message.

package StringFunctions;

import java.util.Scanner;

public class EqualsMethodex {
	private static Scanner sc;
	public static void main(String[] args) {
		sc = new Scanner(System.in);	
		String str = new String("java 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 Java Tutorials");
		}
	}
}
Please Enter any word: 
java programs
Welcome to Java Programming Language

Let us enter different word

Please Enter any word: 
java programming
Goodbye to Java Tutorials

Within the equals String Method example, we declared a String object strand assigned corresponding value.

String str = new String("java programs");

The following statement will ask the user to enter any word. Next, we are going to assign the user entered value to string variable str.

System.out.println("Please Enter any word: ");
String str1 = sc.nextLine();

Next, we used the Java If Else Statement to compare the user entered string with the String Object “java programs” and check whether they are equal or not.

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