JavaStringsBeginner

String.equals()

Compares this string to the specified object. The result is true if and only if the argument is not null and is a String object that represents the same sequence of characters as this object.

Review the syntaxStudy the examplesOpen the coding app
String.equals(Object anObject)

This static page keeps the syntax and examples indexed for search, while the coding app handles interactive exploration and saved references.

What it does

Overview

Compares this string to the specified object. The result is true if and only if the argument is not null and is a String object that represents the same sequence of characters as this object.

The `equals()` method in Java is used to compare two strings for equality based on their content. Unlike the `==` operator, which checks if two references point to the exact same object in memory, `equals()` checks if the character sequences of the two strings are identical. This is crucial for string comparisons, as two different `String` objects can hold the same sequence of characters. The method returns `true` if the strings have the same length and contain the same characters in the same order, and `false` otherwise. It also handles `null` arguments gracefully by returning `false` if `anObject` is `null`, preventing a `NullPointerException`. For case-insensitive comparisons, `equalsIgnoreCase()` should be used. The time complexity is O(n) where `n` is the length of the shorter string, as it compares characters one by one until a mismatch or the end of a string is reached. Always use `equals()` for content comparison of strings.

Quick reference

Syntax

String.equals(Object anObject)

Inputs

Parameters

anObjectObject · The object to compare this String against.

See it in practice

Examples

1

Comparing two strings for content equality

String s1 = "hello";
String s2 = "hello";
String s3 = new String("hello");

System.out.println("s1.equals(s2): " + s1.equals(s2));
System.out.println("s1.equals(s3): " + s1.equals(s3));
System.out.println("s1 == s2: " + (s1 == s2));
System.out.println("s1 == s3: " + (s1 == s3));
Output:
s1.equals(s2): true s1.equals(s3): true s1 == s2: true s1 == s3: false

Demonstrates that `equals()` correctly compares content, while `==` compares references, showing `s1` and `s3` are different objects despite having the same content.

2

Case-sensitive comparison

String city1 = "London";
String city2 = "london";

System.out.println("city1.equals(city2): " + city1.equals(city2));
System.out.println("city1.equalsIgnoreCase(city2): " + city1.equalsIgnoreCase(city2));
Output:
city1.equals(city2): false city1.equalsIgnoreCase(city2): true

Highlights the case-sensitive nature of `equals()` and introduces `equalsIgnoreCase()` for case-insensitive comparisons.

3

Comparing with null and non-String objects

String name = "Alice";
String nullString = null;
Integer number = 123;

System.out.println("name.equals(nullString): " + name.equals(nullString));
System.out.println("name.equals(number): " + name.equals(number));
Output:
name.equals(nullString): false name.equals(number): false

Shows that `equals()` correctly returns `false` when comparing a `String` with `null` or with an object of a different type, without throwing a `NullPointerException`.

Debug faster

Common Errors

1

Using == for String comparison

Cause: The `==` operator compares object references, not their content. For strings, this often leads to unexpected `false` results, especially when strings are created dynamically or from different sources, even if they contain the same characters.

Fix: Always use the `equals()` method (or `equalsIgnoreCase()` for case-insensitive comparison) to compare the content of two String objects.

String s1 = "test";
String s2 = new String("test");

if (s1 == s2) { // This condition is false
    System.out.println("Strings are equal by ==");
} else {
    System.out.println("Strings are not equal by =="); // This will be printed
}

// Fix:
if (s1.equals(s2)) { // This condition is true
    System.out.println("Strings are equal by equals()"); // This will be printed
}

Runtime support

Compatibility

JVMN/AJava 1.0+

Source: Oracle Java Documentation

Common questions

Frequently Asked Questions

Compares this string to the specified object. The result is true if and only if the argument is not null and is a String object that represents the same sequence of characters as this object.

anObject: The object to compare this String against.

Using == for String comparison: Always use the `equals()` method (or `equalsIgnoreCase()` for case-insensitive comparison) to compare the content of two String objects.