HomeLinuxWhat's String equals() in Java

What’s String equals() in Java


In Java, there could be situations the place the developer wants to investigate the contained entries deeply. As an example, within the case of observing the equivalent values having variation of their instances, or evaluating the complicated form of values. In such instances, the “String equals()” methodology in Java is of nice assist in helping the developer in performing the comparisons exactly.

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

What’s “String equals()” in Java?

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

Syntax

On this syntax, “String st” refers back to the string that must be in contrast with the related string.

Instance 1: Making use of “String equals()” in Java to Examine the Specified String Values

On this instance, the “String equals()” methodology could be utilized to check the desired string values:

public class stringequals {
 public static void fundamental(String args[]) {
  String firstString = “Linux”;
  String secondString = “trace”;
  String thirdString = “linux”;
  String fourthString = “trace”;
  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));
}}

Within the above code snippet, apply the next steps:

  • Initialize the acknowledged string values named “firstString”, “secondString”, “thirdString”, and “fourthString”, respectively.
  • After that, affiliate the “equals()” methodology and examine the desired strings with each other at every step.
  • Lastly, log the corresponding consequence based mostly on the comparability as a “boolean” worth.

Output

On this output, it may be analyzed that the second consequence returned “false” regardless of the identical string values. This signifies that the mentioned methodology is “case-sensitive” such that “L” doesn’t equal “l”.

Earlier than continuing to the subsequent instance, make certain to incorporate the below-provided bundle to allow person enter:

import java.util.Scanner;

Instance 2: Making use of “String equals()” in Java to Examine the Consumer Enter String Values

On this instance, the mentioned methodology could be utilized to check the person enter string values and return the corresponding consequence accordingly:

public class stringequals2 {
 public static void fundamental(String args[]) {
  Scanner object = new Scanner(System.in);
  System.out.println(“Enter the primary 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 should not equal!”);
}
}}

In line with the above strains of code, carry out the below-provided steps:

  • First, create a “Scanner” object utilizing the “new” key phrase and the “Scanner()” constructor, respectively.
  • The “in” parameter reads the enter and the “nextLine()” methodology takes the person enter as a “string”.
  • Now, affiliate the “equals()” methodology with the person enter strings, think about the exception as a “boolean”, and invoke the corresponding “if/else” assertion.

Output

This output implies that the person enter strings are in contrast and the corresponding assertion is displayed accordingly.

Conclusion

The “String equals()” methodology compares two strings and returns the corresponding boolean values based mostly on the equal or unequal strings. This methodology is case-sensitive and could be utilized to check the desired or person enter strings. This text mentioned the utilization and implementation of the “String equals()” methodology in Java.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments