In AP Computer Science A, a null comparison uses the relational operators == or != to test whether a variable or array element references null, letting you safely guard against calling a method on an object that doesn't exist.
A null comparison is a Boolean expression like myObj == null or myObj != null. It checks whether a reference variable actually points to an object or points to nothing at all. Under EK 2.2.A.1, the operators == and != compare object references for reference types, not object contents. That's exactly why null comparisons work. You're asking "does this variable reference the same thing as null?" and the answer is a clean true or false.
The whole point is safety. If a variable is null and you call a method on it, your program crashes with a NullPointerException. Writing if (name != null) before calling name.length() is the guard rail. Notice the direction matters too. name == null is fine because == never calls a method, but name.equals(null) will crash if name is null, since you can't call .equals() on nothing.
Null comparison lives in Topic 2.2 (Boolean Expressions) in Unit 2: Selection and Iteration, and it directly supports learning objective AP Comp Sci A 2.2.A, which asks you to build Boolean expressions with relational operators and predict their results. It's also the one place where the reference-vs-value distinction in EK 2.2.A.1 becomes immediately practical. With primitives, == compares values. With objects, it compares references, and null comparison is the most common reason you'll ever want a reference comparison on purpose. From Unit 2 onward, almost every program you write that searches an array, builds an ArrayList algorithm, or processes object data will need a null check somewhere, so this small expression keeps showing up in MCQ code traces all year.
Keep studying AP® Computer Science A Unit 2
== (equality operator) (Unit 2)
Null comparison is the one case where using == on objects is the correct move. Since == compares references and null is the absence of a reference, x == null does exactly what you want with zero crash risk.
Object reference (Unit 1)
You can't understand null comparison without the reference model. A reference variable is like an address card, and null means the card is blank. Comparing to null asks whether the card points anywhere at all.
Equality comparison (Unit 2)
Equality comparison splits into two flavors. The == operator checks if two variables reference the same object, while .equals() checks if contents match. Null comparison only ever uses ==, because calling .equals() on a null variable throws a NullPointerException.
Relational operator (Unit 2)
Null comparison uses only == and != from the relational operator family. The ordering operators <, >, <=, and >= work on numeric values per EK 2.2.A.2, so something like x < null isn't legal Java.
No released FRQ uses the phrase "null comparison" directly, but the skill is baked into the exam everywhere objects appear. Multiple-choice questions love code traces where a variable might be null, and you have to spot whether a method call will throw a NullPointerException or whether a guard like if (s != null && s.length() > 0) short-circuits safely. On FRQs, especially array and ArrayList questions, checking for null before calling a method is often the difference between earning the point and losing it to a runtime error. The habit to build is simple. Any time your code calls a method on a variable that could be null, write the != null check first, and put it on the left side of an && so short-circuit evaluation protects the method call.
Use == or != to compare against null, and use .equals() to compare object contents. The trap is direction. name == null always works because == just compares references, but name.equals("hi") crashes if name is null, because you can't call a method on a nonexistent object. That's why careful code checks name != null before any .equals() call.
A null comparison like x == null or x != null checks whether a variable references an object at all, and it always evaluates to a Boolean value.
Per EK 2.2.A.1, == and != compare references for object types, which is exactly why == is the correct tool for null checks instead of .equals().
Calling any method on a null variable throws a NullPointerException, so the null check has to come before the method call.
Put the null check on the left of && so short-circuit evaluation skips the risky method call, as in if (s != null && s.length() > 0).
Only == and != work with null; the ordering operators <, >, <=, and >= are for numeric values and won't compile with null.
It's a Boolean expression using == or != to check whether a variable references null, such as if (obj != null). It's covered in Topic 2.2 as part of building Boolean expressions with relational operators.
No, and this is a classic trap. If the variable is null, calling .equals() on it throws a NullPointerException because there's no object to call the method on. Always use variable == null instead.
The == operator compares references, so x == null safely asks whether x points to nothing. The .equals() method compares object contents and requires an actual object to call it on, so it can never be your null check.
No. The expressions x == null and x != null never throw exceptions because == doesn't call any methods. The crash only happens when you call a method on a variable that is actually null.
Java's && short-circuits, meaning if s != null is false, the right side never runs. Flip the order and s.length() executes first, throwing a NullPointerException whenever s is null.
Connect this key term to the AP exam workflow: review the course, practice questions, and check related study tools.
Review units, study guides, and course resources.
Check this vocabulary in multiple-choice context.
Apply key concepts in written AP responses.
Estimate the exam score you are working toward.
Review the highest-yield facts before practice.
Put the full course together before test day.