Skip to main content
The new Teacher Workspace is here. Your first 3 assignments are free. Try it →
A colleague sent you $30 off Fiveable. Get the annual plan for $99 →

Null in AP Computer Science A

In AP Computer Science A, null is a special literal that means a reference variable is not associated with any object. Only reference types (like String or Student) can hold null, and you check for it with == null or != null before calling methods on the reference.

Verified for the 2027 AP Computer Science A exam•Last updated October 2026

What is null?

In Java, a variable of a reference type doesn't hold the object itself. It holds a reference, kind of like an address that tells Java where the object lives. Null is what that variable holds when there's no address to give. Per EK 1.13.B.1, a reference variable holds either an object reference or, if there is no object, null. EK 1.4.A.1 adds that reference types can be assigned a new object or null, and that the literal null is a special value meaning "this reference isn't connected to any object."

Think of a reference variable as a TV remote. new Student("Ana", 16) pairs the remote with a TV. null is a remote that isn't paired with anything. You can hold it, pass it around, and check whether it's paired. But pressing a button (calling a method like s.getName()) does nothing useful, and Java throws a NullPointerException. Only reference types can be null. A primitive like int or double always holds an actual value, so int x = null; won't compile.

Why null matters in AP® Computer Science A

Null sits at the seam between Unit 1: Using Objects and Methods and Unit 2: Selection and Iteration. In Unit 1, it's part of how you declare and initialize reference variables (Topic 1.4, AP Comp Sci A 1.4.A, and Topic 1.13, AP Comp Sci A 1.13.B). In Unit 2, it becomes something you test. EK 2.6.B.2 says an object reference can be compared with null using == or != to find out whether it actually references an object (AP Comp Sci A 2.6.B). That makes null the reason Boolean expressions about objects exist in the first place. Once you can ask "is this reference pointing at anything?", you can write the safe guard conditions that FRQ code depends on.

How null connects across the course

Creating and Storing Objects (Unit 1)

Declaring Student s; and creating s = new Student("Ana", 16); are two separate steps. Until the constructor call with new runs, there's no object for s to refer to. Null is the name for that "declared but not yet connected" state, and it's why declaring a variable and creating an object are tested as different skills (1.13.B vs. 1.13.C).

Assignment Statements and Initialization (Unit 1)

Every variable has to be assigned a value before you use it (EK 1.4.A.1). For reference types, null counts as a legal value to assign. So String name = null; is initialized, even though it refers to nothing. Being initialized and pointing at an object are not the same thing.

Comparing References with == vs. equals (Unit 2)

== asks whether two references point to the same object. equals asks whether two objects are equivalent by the class's own criteria (EK 2.6.B.1, 2.6.B.3). Null fits the == side. You write s == null, never s.equals(null) to test a possibly-null s, because calling a method on a null reference crashes before any comparison happens.

De Morgan's Law and Null Guards (Unit 2)

A null check usually lives inside a compound condition like s != null && s.getName().equals("Ana"). De Morgan's law tells you its negation is s == null || !s.getName().equals("Ana"). Flipping guard conditions correctly is exactly the kind of equivalent-expression reasoning 2.6.A tests.

Is null on the AP® Computer Science A exam?

Null shows up mostly in multiple-choice code tracing and quietly inside FRQ method logic. Expect MCQ stems that:

  • Ask what a reference variable holds after declaration but before a constructor call.
  • Compare two references with == and ask whether the result is true or false. Questions about methods like compareStudents(Student s1, Student s2) often state that the references are "non-null", which signals that null would otherwise break the code.
  • Ask which condition safely checks an object before calling a method on it.
  • Test equivalent Boolean expressions where one part is a null check.

On free response, null usually appears as a return value meaning "nothing found" or as a condition you need to guard against. The 2017 FRQ set (including Digits, StudyPractice, Phrase, and Successors) is a good place to see how method specs describe return values and preconditions. What you need to do: recognize when a reference might be null, check it with == null or != null first, and never call a method on it until you know it points to an object.

Null vs Empty String

An empty string "" is a real String object that happens to contain zero characters. You can call "".length() and get 0. null means there's no String object at all, so calling .length() on a null reference throws a NullPointerException. Here's a quick picture: "" is an empty box, and null is no box. On the exam, str == null and str.equals("") test completely different things.

Key things to remember about null

  • Null is a literal value that means a reference variable is not associated with any object.

  • Only reference types like String, Student, or ArrayList can be null, while primitives like int, double, and boolean cannot.

  • You check whether a reference points to an object using == null or != null, as described in EK 2.6.B.2.

  • Calling a method on a null reference causes a NullPointerException, so you put the null check first in a condition like s != null && s.getName().equals(x).

  • Assigning null to a reference variable counts as initializing it, even though it doesn't refer to an object yet.

  • An empty string is an actual String object with length 0, which makes it completely different from null.

Frequently asked questions about null

What is null in AP Computer Science A?

Null is a special literal that means a reference variable doesn't point to any object. EK 1.13.B.1 says a reference variable holds either an object reference or, if there is no object, null.

Can an int be null in Java?

No. Primitives like int, double, and boolean always hold an actual value, so int x = null; won't compile. Only reference types such as String or a class you write can be assigned null.

Is null the same as an empty string?

No. "" is a real String object with length 0, so you can call methods on it. null means there's no object at all, and calling .length() on it throws a NullPointerException.

Should I use == or .equals() to check for null?

Use == or !=, as EK 2.6.B.2 specifies. Writing s.equals(null) is a trap, because if s is null, the method call itself crashes before any comparison happens.

Why does the order matter in s != null && s.getName().equals(x)?

Java evaluates the left side first. If s is null, the && stops there and never calls getName(). Swap the order and the method call runs on a null reference, which throws a NullPointerException.