Java String equals() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
String

Definition

In Java, equals() checks whether two strings have the same characters. It returns true or false.

You pass the other value. It is required for a normal call. Do not use == to compare string text. == compares references.

Syntax

Call equals on the string. Start with this line.

java
boolean ok = a.equals(b);
CallWhat it does
a.equals(b)Returns true when a and b have the same characters.

"Java".equals(null) returns false. Prefer writing the literal first when one side might be null.

Library declarations
java
public boolean equals(Object anObject);

Parameters

ParameterDescription
anObjectRequired. The other string or object to compare.

Return Value

Return ValueDescription
trueThe characters match.
falseThe characters do not match, or the argument is null.

Java Examples

Example 1 — Compare Content

Check whether two strings contain the same text.

java
public class EqualsBasic {
    public static void main(String[] args) {
        String a = "Java";
        String b = "Java";
        String c = "java";

        System.out.println(a.equals(b));
        System.out.println(a.equals(c));
    }
}

How It Works

  1. Same text. a and b are both "Java" → true.
  2. Case differs. "Java" vs "java" → false.
  3. Character by character. equals compares every letter in order.
  4. Boolean result. Use it directly in println or an if.

Example 2 — equals vs ==

new String(...) creates a new object. Content can match even when == is false.

java
public class EqualsVsEqEq {
    public static void main(String[] args) {
        String a = "Hi";
        String b = new String("Hi");

        System.out.println(a == b);
        System.out.println(a.equals(b));
    }
}

How It Works

  1. Two objects. Literal "Hi" and new String("Hi") are not the same reference.
  2. == is false. It asks “same object?” — here, no.
  3. equals is true. Both hold the characters H and i.
  4. Rule of thumb. Compare string text with equals, not ==.

Example 3 — Null-Safe Literal Style

Put the constant on the left so a null variable does not crash. For case-insensitive checks, see equalsIgnoreCase.

java
public class EqualsNullSafe {
    public static void main(String[] args) {
        String input = null;

        // safe: returns false
        System.out.println("yes".equals(input));

        // unsafe if uncommented:
        // System.out.println(input.equals("yes")); // NPE
    }
}

How It Works

  1. input is null. Calling a method on it would throw.
  2. Literal first. "yes".equals(input) runs on a real string.
  3. Null argument. equals(null) returns false safely.
  4. Avoid the reverse. input.equals("yes") throws NullPointerException.

Frequently Asked Questions

equals() checks whether two strings have the same characters and returns true or false.
== compares references. equals() compares character content.
Yes.
It returns false when called on a non-null string.
Calling equals on null throws NullPointerException.

Next: equalsIgnoreCase()

Compare two strings without caring about letter case.

equalsIgnoreCase() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful