THE WORLD'S LARGEST WEB DEVELOPER SITE

Java String equals() Method

❮ String Methods


Example

Compare strings to find out if they are equal:

public class MyClass {
  public static void main(String[] args) {
    String myStr1 = "Hello";
    String myStr2 = "Hello";
    String myStr3 = "Another String";
    System.out.println(myStr1.equals(myStr2)); // Returns true because they are equal
    System.out.println(myStr1.equals(myStr3)); // false
  }
}

Run example »


Definition and Usage

The equals() method compares two strings, and returns true if the strings are equal, and false if not.

Tip: Use the compareTo() method to compare two strings lexicographically.


Syntax

public boolean equals(Object anotherObject)

Parameter Values

Parameter Description
anotherObject An Object, representing the other string to be compared

Technical Details

Returns: A boolean value:
  • true - if the strings are equal
  • false - if the strings are not equal
Overrides: equals in class Object

❮ String Methods