Java

How to Use the equals Method in Java

In Java programming, there can be instances where the programmer wants to compare the specified or user input values in order to utilize them. More specifically, while dealing with complex sorts of values that are somewhat tricky to identify or analyze the randomly generated values. In such situations, the “equals()” method in Java is assistive in comparing the values efficiently.

This blog will elaborate on using and applying the “equals()” method in Java.

How to Use the “equals()” Method in Java?

The “equals()” method compares two values, i.e., “Integer”, “String”, “Object”, etc., and returns the corresponding outcome in the form of a boolean value, i.e., “true/false”.

Syntax

public boolean equals(Object obj)

 
In this syntax, “Object obj” is the reference object with which the current “String” object needs to be compared. However, this method compares the values comprising multiple data types.

Example 1: Using the “equals()” Method With the “Integer Values” in Java

In this example, the “equals()” method can be applied to compare the specified integer values:

public class equals {
 public static void main( String args[] ) {
  Integer num1 = 3;
  Integer num2 = 6;
  Integer num3 = 6;
  System.out.print(num1.equals(num2)+ "\n");
  System.out.print(num2.equals(num3));
}}

 
In the above lines of code, apply the following steps:

    • Firstly, initialize the stated three integer values.
    • After that, apply the “equals()” method with the first two integers and return the corresponding outcome as a boolean value.
    • Likewise, apply a check upon the last two integers and display the resultant output.

Output


In this output, it can be seen that based on the equivalent values, the method returned the boolean value “true”.

Before heading to the next example, make sure to include the below-provided package to enable user input:

import java.util.Scanner;

 
Example 2: Using the “equals()” Method With the User Input “String Values” in Java

This example utilizes the discussed method to apply a check on the user input strings:

public class equals2 {
 public static void main(String args[]) {
  Scanner object = new Scanner(System.in);
  System.out.println("Enter the first string: ");
  String val1 = object.nextLine();
  System.out.println("Enter the second string: ");
  String val2 = object.nextLine();
  System.out.println(val1.equals(val2));
  object.close();
}}

 
According to this code, perform the below-given steps:

    • First, create a “Scanner” object using the “new” keyword and the “Scanner()” constructor, respectively.
    • The “System.in” parameter enables the user input.
    • Now, input the two strings from the user one by one.
    • The “nextLine()” method ensures the user input value as a “String”.
    • Lastly, apply the “equals()” method upon the user input strings, return the corresponding boolean outcome, and close the scanner via the “close()” method.

Output


 

In this output, the following aspects can be noticed:

    • The identical strings return the boolean value “true”.
    • In the other case, “false” is returned.
    • The mismatched case in both the compared strings leads to a “false” outcome.

Example 3: Using the “equals()” Method With the “Boolean Values” in Java

In this particular example, the discussed method can be implemented to compare the initialized boolean values and the created object:

public class equals3 {
 public static void main(String args[]) {
  Boolean value1 = true;
  Boolean value2 = false;
  Boolean object = new Boolean(true);
  System.out.println(value1.equals(value2));
  System.out.println(value1.equals(object));
}}

 
In this code block:

    • Initialize the two provided boolean values.
    • In the next step, create a “Boolean” object via the “new” keyword and the “Boolean()” constructor, respectively.
    • In the constructor parameter, specify the stated boolean value.
    • Finally, apply the “equals()” method to first compare the initialized boolean values and then apply a comparison upon the former initialized value and the object.

Output


In this outcome, it can be implied that the initialized value is compared appropriately with the created object.

Example 4: Using the “equals()” Method With the “String Objects” in Java

This example will use the discussed method to compare the created string objects without and with allocating the two string values, respectively:

public class Example {
 public static void main(String args[]) {
  String object1 = new String();
  String object2 = new String();
  System.out.println(object1.equals(object2));
  object1 = "Linux";
  object2 = "hint";
  System.out.println(object1.equals(object2));
}}

 
In the above code snippet:

    • First, create the two “String” objects named “object1” and “object2”, respectively via the discussed approach.
    • In the next step, associate the “equals()” method with the former object and place the latter object as its(method) parameter to check if the created objects are equal before assigning the values.
    • After that, allocate the stated string values to both the created objects.
    • Lastly, compare both objects again after the values are assigned to observe the change in comparison.

Output


In this output, it can be analyzed that since the objects were not allocated with the values first, so the boolean value “true” is returned, which is not the case in the latter scenario(after the values are set).

Conclusion

The “equals()” method in Java can be used to compare two values, i.e., “Integer”, “String”, “Object”, etc., and return the corresponding outcome as a boolean value, i.e., “true/false”. This method is case-sensitive and can be applied to the user input as well as specified values comprising multiple data types. This article demonstrated using and implementing the “equals()” method in Java.

About the author

Umar Hassan

I am a Front-End Web Developer. Being a technical author, I try to learn new things and adapt with them every day. I am passionate to write about evolving software tools and technologies and make it understandable for the end-user.