Skip to main content
United StatesComputer ScienceSyllabus dot point

How do you compare objects in Java, and why does == differ from the equals method?

Topic 3.7 Comparing Objects: compare object references with == and !=, compare object contents with equals, and detect a null reference, understanding the difference between identity and equality.

A focused answer to AP CSA Topic 3.7, covering reference equality with == and !=, content equality with the equals method, comparing against null, why two equal-looking objects can be unequal by ==, and using equals/compareTo for Strings, with a fully worked example.

Generated by Claude Opus 4.810 min answer

Reviewed by: AI editorial process; not yet individually human-reviewed

Have a quick question? Jump to the Q&A page

Jump to a section
  1. What this topic is asking
  2. Reference equality: == and !=
  3. Content equality: the equals method
  4. Why two equal-looking objects differ by ==
  5. Comparing against null
  6. Try this

What this topic is asking

The College Board (Topic 3.7) wants you to compare objects correctly. The == and != operators compare references (whether two variables point to the same object in memory), while the equals method compares contents (whether two objects are logically the same). You also need to test a reference against null. Confusing reference identity with content equality is one of the most heavily tested traps on the exam.

Reference equality: == and !=

So a == b answers "are these the same object?", not "do these objects look the same?". The one common, correct use of == with objects is comparing against null: if (node == null) checks whether a reference points to nothing.

Content equality: the equals method

This is why String comparison must use equals, never ==. The same idea applies to other objects whose class defines equals to compare meaningful state. When ordering matters, String also offers compareTo, which returns a negative number, zero, or a positive number by dictionary order (Topic 2.7).

Why two equal-looking objects differ by ==

When you write new, Java creates a brand-new object with its own location in memory. So new String("cat") and new String("cat") are two objects: == sees two different references and returns false, while equals inspects the characters and returns true. The lesson: to ask "do these hold the same value?", always use equals.

Comparing against null

A null reference points to no object. Calling any method on it - for example s.equals("x") when s is null - throws a NullPointerException. Guard with a null check first, exploiting short-circuit evaluation:

if (s != null && s.equals("x")) { /* safe */ }

Because && short-circuits, s.equals("x") only runs when s != null is true.

Try this

Q1. State what a == b tests when a and b are object references. [1 point]

  • Cue. Whether a and b refer to the same object (reference identity), not whether their contents match.

Q2. Explain why s.equals("hi") may crash and how to make the comparison safe. [2 points]

  • Cue. If s is null, calling equals on it throws a NullPointerException; guard with s != null && s.equals("hi"), or write "hi".equals(s) so the method is called on the non-null literal.

Exam-style practice questions

Practice questions written in the style of College Board exam questions on this dot point, with worked answer explainers. The year tag is the paper they imitate, not the source.

AP 2021 (style)1 marksMultiple choice. Consider the following code segment. ```java String a = new String("cat"); String b = new String("cat"); System.out.println(a == b); System.out.println(a.equals(b)); ``` What is printed? (A) `false` then `true` (B) `true` then `true` (C) `false` then `false` (D) `true` then `false` (E) Nothing; a compile-time error occurs.
Show worked answer →

The answer is (A).

a and b are created with new, so they are two distinct objects even though they hold the same characters. a == b compares references - whether they point to the same object - which is false because they are different objects. a.equals(b) compares contents, which is true because both hold "cat". So the output is false then true. (B) wrongly assumes == compares contents; (D) reverses the two results.

Markers reward knowing that == tests reference identity while equals tests content equality.

AP 2020 (style)3 marksFree response (code writing). A `String` parameter `input` may be `null`. Write a code segment that safely prints `"match"` when `input` is not null and equals the String `"yes"`, and prints `"no match"` in every other case (including when `input` is null). Avoid a NullPointerException.
Show worked answer →

A 3-point question testing null safety and equals.

if (input != null && input.equals("yes")) {
  System.out.println("match");
} else {
  System.out.println("no match");
}

Point 1: the null check input != null is placed first in the &&. Point 2: short-circuit evaluation means input.equals("yes") is only called when input is not null, so no NullPointerException. Point 3: the else covers null and any non-matching value, printing "no match". Writing input.equals("yes") first would crash when input is null. An alternative safe idiom is "yes".equals(input), which never throws because the literal is never null.

Related dot points

Sources & how we know this