Skip to content
Tutorialbeginner · 2 min read · 2026-11-13

Java String Comparison: `equals()` vs `==`

Learn why two Java strings can print the same text but fail a `==` comparison, and how to compare string content safely with `equals()` or `Objects.equals()`.

This lesson goes with our YouTube Short. Watch it, then work through the example and the practice below.

Subscribe on YouTube

Open on YouTube

What you will learn

  • After this lesson you can explain the difference between object identity and string content in Java.
  • After this lesson you can choose equals() instead of == for normal string comparison.
  • After this lesson you can avoid NullPointerException when comparing possibly null strings.
  • After this lesson you can recognize why string literals sometimes make == appear to work.

The core difference

In Java, == compares whether two reference variables point to the exact same object. It answers: “Are these the same object in memory?”

equals() is a method. For String, it compares the sequence of characters. It answers: “Do these strings contain the same text?”

For almost all application logic, string comparison means content comparison, so use equals().

Why the interview trap happens

Two strings can look identical when printed but still be different objects. For example, "java" and new String("java") both contain the same four characters, but new String(...) creates a separate object.

String literals are special: Java can reuse the same literal object. That is why "java" == "java" may print true. This does not make == a content comparison; it only means both variables happen to refer to the same object.

Runnable example

Run this program mentally before checking the output. The first comparison asks about object identity. The second asks about string content.

The last two lines show why beginners get confused: two literals can share one interned object, so == can appear to work in that narrow case.

Safe comparison when null is possible

Calling a method on null throws NullPointerException. So name.equals("Ana") is unsafe if name might be null.

For comparing with a constant, write "Ana".equals(name). If both sides are variables and either may be null, use Objects.equals(a, b) from the standard library.

Rule of thumb

Use == for primitive values like int, boolean, and char, or when you truly need to know whether two references point to the same object.

Use equals() for string text comparison. If null is possible, use a null-safe pattern.

Code

Java · Same text, different string objects✓ Executed & verified · javac 21.0.13
public class Main {
    public static void main(String[] args) {
        String literal = "java";
        String created = new String("java");
        String anotherLiteral = "java";

        System.out.println(literal == created);
        System.out.println(literal.equals(created));
        System.out.println(literal == anotherLiteral);
        System.out.println(literal.equals(anotherLiteral));
    }
}

literal == created is false because the variables refer to different objects. literal.equals(created) is true because both strings contain the same characters. The two literals refer to the same interned literal object, so their == comparison is true.

Java · Null-safe string comparison✓ Executed & verified · javac 21.0.13
import java.util.Objects;

public class Main {
    public static void main(String[] args) {
        String role = null;
        String expected = "admin";

        System.out.println("admin".equals(role));
        System.out.println(Objects.equals(role, expected));
        System.out.println(Objects.equals("admin", expected));
    }
}
Output (real run)
false
false
true

"admin".equals(role) is safe because the method is called on a non-null literal. Objects.equals is also null-safe and works when either argument may be null.

Common mistakes

  • ✕ Using == to check whether two strings contain the same characters.

    ✓ Use a.equals(b) when a is known to be non-null, or Objects.equals(a, b) when either value may be null.

  • ✕ Thinking == is reliable because it works for two string literals like "java" == "java".

    ✓ Remember that literals can refer to the same interned object; strings built at runtime may be different objects with the same content.

  • ✕ Calling input.equals("yes") when input might be null.

    ✓ Use "yes".equals(input) for a constant comparison, or use Objects.equals(input, "yes").

Interview questions

Why can "abc" == "abc" be true in Java?

String literals can be reused from the string pool, so both references may point to the same literal object. This is still an identity comparison, not a content comparison.

Why is new String("abc") == "abc" usually false?

new String("abc") creates a distinct String object. It has the same content as the literal, but it is not the same object reference.

What is the correct way to compare two non-null strings by content?

Use a.equals(b). For String, equals() compares the character sequence.

How do you compare two strings safely if either may be null?

Use Objects.equals(a, b), which returns true if both are null, false if only one is null, and otherwise calls equals().

Practice

  1. Create two strings with the same text: one as a literal and one using new String(...). Print the results of == and .equals().
  2. Write a method isAdmin(String role) that returns true only when the role text is admin, without throwing if role is null.
  3. Try comparing two strings built using concatenation. Which comparisons are identity checks, and which are content checks?

Quick quiz

Q1. What does `a == b` check when `a` and `b` are `String` variables?
Q2. What is the exact output of snippet s1?
Q3. Which expression is safest if `name` might be null and you want to check whether it is `"Ravi"`?
Q4. When should you use `==` with strings?

Key takeaways

  • == compares object identity for strings, not text content.
  • String.equals() compares the characters inside the strings.
  • String literals can make == look correct because literals may share the same object.
  • Use "constant".equals(value) or Objects.equals(a, b) when null is possible.
  • Use == with strings only when you intentionally need reference identity.

References

Related YouTube lessons

See the full Java learning path

Keep learning