bugl
bugl
HomeLearnPatternsPathsSearchPremium
HomeLearnPatternsPaths

Loading lesson path

Learn/Java/Java Reference
Java•Java Reference

Java String equals() Method

❮ String Methods

Example

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

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

ParameterDescription
anotherObjectAn 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
  • true - if the strings are equal
  • false - if the strings are not equal

Previous

Java String endsWith() Method

Next

Java String equalsIgnoreCase() Method