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.
Reviewed by: AI editorial process; not yet individually human-reviewed
Have a quick question? Jump to the Q&A page
Jump to a section
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
aandbrefer 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
sisnull, callingequalson it throws a NullPointerException; guard withs != 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
- Topic 2.7 String Methods: call the String methods in the AP Java subset (length, substring, indexOf, equals, compareTo), respecting zero-based indexing and the immutability of String objects.
A focused answer to AP CSA Topic 2.7, covering the required String methods length, substring (both forms), indexOf, equals and compareTo, zero-based indexing, the half-open range of substring, why == differs from equals, and String immutability, with a worked trace.
- Topic 3.5 Compound Boolean Expressions: combine boolean expressions with the logical operators && (and), || (or) and ! (not), applying short-circuit evaluation.
A focused answer to AP CSA Topic 3.5, covering the logical operators && , || and !, their truth tables, operator precedence and short-circuit evaluation, why short-circuiting prevents errors, and how to trace a compound condition, with a fully worked example.
- Topic 3.1 Boolean Expressions: evaluate expressions formed with the relational operators (<, >, <=, >=, ==, !=) that produce a boolean result of true or false.
A focused answer to AP CSA Topic 3.1, covering the six relational operators, how they compare numeric primitives to produce a boolean, the difference between == and =, comparing doubles, and how relational operators combine with arithmetic, with a fully traced worked evaluation.
- Topic 3.3 if-else Statements: use a two-way if-else statement so that exactly one of two code blocks runs depending on whether the boolean condition is true or false.
A focused answer to AP CSA Topic 3.3, covering two-way selection with if-else, the guarantee that exactly one branch runs, how the else attaches to the nearest if, nested if-else, and how to trace each branch, with a fully worked example.
- Topic 2.1 Objects: Instances of Classes: explain the relationship between a class and its objects, and describe an object as an instance of a class with state and behavior.
A focused answer to AP CSA Topic 2.1, covering the class-object relationship, what it means for an object to be an instance, the difference between attributes (state) and methods (behavior), and reference versus primitive variables, with a worked example.
Sources & how we know this
- AP Computer Science A Course and Exam Description — College Board (2025)