Java

What is String equals() in Java

In Java, there can be instances where the developer needs to analyze the contained entries deeply. For instance, in the case of observing the identical values having variation in their cases, or comparing the complex sort of values. In such cases, the “String equals()” method in Java is of great aid in assisting the developer in performing the comparisons precisely.

This write-up will elaborate on using and implementing the “String equals()” method in Java.

What is “String equals()” in Java?

The “String equals()” method compares two strings and returns the corresponding boolean values based on the equivalent or unequal strings.

Syntax

string.equals(String st)

In this syntax, “String st” refers to the string that needs to be compared with the associated string.

Example 1: Applying “String equals()” in Java to Compare the Specified String Values

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

public class stringequals {
 public static void main(String args[]) {
  String firstString = "Linux";
  String secondString = "hint";
  String thirdString = "linux";
  String fourthString = "hint";
  System.out.println("Are the values equal? "+firstString.equals(secondString));
  System.out.println("Are the values equal? "+firstString.equals(thirdString));
  System.out.println("Are the values equal? "+secondString.equals(fourthString));
}}

In the above code snippet, apply the following steps:

  • Initialize the stated string values named “firstString”, “secondString”, “thirdString”, and “fourthString”, respectively.
  • After that, associate the “equals()” method and compare the specified strings with one another at each step.
  • Finally, log the corresponding outcome based on the comparison as a “boolean” value.

Output

In this output, it can be analyzed that the second outcome returned “false” despite the same string values. This signifies that the discussed method is “case-sensitive” such that “L” does not equal “l”.

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

import java.util.Scanner;

Example 2: Applying “String equals()” in Java to Compare the User Input String Values

In this example, the discussed method can be utilized to compare the user input string values and return the corresponding outcome accordingly:

public class stringequals2 {
 public static void main(String args[]) {
  Scanner object = new Scanner(System.in);
  System.out.println("Enter the first string: ");
  String first = object.nextLine();
  System.out.println("Enter the second string: ");
  String second =object.nextLine();
  if (first.equals(second) == true) {
   System.out.println("The values are equal!");
}
  else {
   System.out.println("The values are not equal!");
}
}}

According to the above lines of code, perform the below-provided steps:

  • First, create a “Scanner” object using the “new” keyword and the “Scanner()” constructor, respectively.
  • The “in” parameter reads the input and the “nextLine()” method takes the user input as a “string”.
  • Now, associate the “equals()” method with the user input strings, consider the exception as a “boolean”, and invoke the corresponding “if/else” statement.

Output

This output implies that the user input strings are compared and the corresponding statement is displayed accordingly.

Conclusion

The “String equals()” method compares two strings and returns the corresponding boolean values based on the equivalent or unequal strings. This method is case-sensitive and can be applied to compare the specified or user input strings. This article discussed the usage and implementation of the “String 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.