---
title: "Reference Variable — AP Comp Sci A Definition & Exam Guide"
description: "A reference variable stores the memory address of an object, not the object itself."
canonical: "https://fiveable.me/ap-comp-sci-a/key-terms/reference-variable"
type: "key-term"
subject: "AP Computer Science A"
---

# Reference Variable — AP Comp Sci A Definition & Exam Guide

## Definition

In AP Computer Science A, a reference variable is a variable that stores the memory address of an object rather than the object itself, letting you access that object's methods and instance variables; it's how all non-primitive (reference) types work in Java.

## What It Is

A reference variable doesn't hold an [object](/ap-comp-sci-a/key-terms/object "fv-autolink"). It holds the [memory address](/ap-comp-sci-a/unit-1/objects-instances-of-classes/study-guide/EcpFHGcIKu6385hMohLe "fv-autolink") where the object lives, like a sticky note with a house address written on it instead of the house itself. When you write `Dog d = new Dog();`, the `new` keyword builds the actual Dog object in memory, and `d` just stores directions to it. You then use `d` with the dot operator to call methods and access the object's data.

This one idea explains a lot of Java behavior that trips people up. Two reference [variables](/ap-comp-sci-a/unit-1/expressions-and-assignment-statements/study-guide/01dr6uUPDAn3SjtK2Psr "fv-autolink") can point to the same object, so changing the object through one variable changes what the other one sees. A reference variable can also hold `null`, which means it points to nothing at all, and calling a method on it throws a `NullPointerException`. Every variable in Java is either a primitive type (`int`, `double`, `boolean`) or a reference type, and reference variables are how the reference side works.

## Why It Matters

[Reference](/ap-comp-sci-a/key-terms/reference "fv-autolink") variables show up the moment you start using objects in AP CSA and never leave. They're behind how `new` and [constructors](/ap-comp-sci-a/unit-3/constructors/study-guide/3Ez6zzak2wRwMrTj2ZQk "fv-autolink") work, why `null` exists, how objects get passed to methods (the method receives a copy of the reference, so it can modify the original object), and why `==` compares addresses while `.equals()` compares contents.

## Connections

### Object and the New Keyword (Unit 2)

The `new` keyword creates the object in memory and hands back its address, and the reference variable is where that address gets stored. Declaration makes the sticky note; `new` builds the house and writes the address on it.

### null and Null Reference (Unit 2)

A reference variable set to `null` points to no object at all. Calling a method through a [null reference](/ap-comp-sci-a/key-terms/null-reference "fv-autolink") causes a `NullPointerException`, one of the most-tested runtime errors in MCQs.

### Garbage Collection (Unit 2)

When no reference variable points to an object anymore, Java's garbage collector reclaims that memory automatically. Reassigning a reference can leave its old object unreachable, which is exactly when garbage collection kicks in.

## On the AP Exam

Reference variables get tested in two main ways. In Units 1-2-style MCQs, you'll trace code where two references point to the same object, or where a null reference causes a `NullPointerException`, or where `==` returns false for two objects with identical contents. On FRQs, no released question uses the phrase "reference variable" verbatim, but every FRQ that has you write or use a class depends on you handling references correctly, especially when a method parameter is an object and your changes need to (or must not) affect the original.

## Reference Variable vs Primitive Variable

A primitive variable (`int`, `double`, `boolean`) stores the actual value directly, so copying it makes a truly independent copy. A reference variable stores an address, so copying it gives you a second variable pointing at the same object. That's why `int x = y;` then changing `y` doesn't touch `x`, but two references to the same object always see each other's changes. It's also why primitives can never be `null` but references can.

## Key Takeaways

- A reference variable stores the memory address of an object, not the object itself, and you use it with the dot operator to call the object's methods.
- Assigning one reference variable to another copies the address, so both variables point to the same object and changes through either one are visible through both.
- A reference variable holding null points to no object, and calling any method on it throws a NullPointerException.
- A superclass reference variable can hold a subclass object (Shape s = new Rectangle();), but a subclass reference cannot hold a superclass object.
- The == operator compares whether two reference variables hold the same address, while .equals() compares the contents of the objects.
- When an object has no reference variables pointing to it, Java's garbage collector automatically frees its memory.

## FAQs

### What is a reference variable in AP Computer Science A?

It's a variable that stores the memory address of an object instead of the object itself. In `Dog d = new Dog();`, the object lives in memory and `d` just holds directions to it, which you follow using the [dot operator](/ap-comp-sci-a/key-terms/dot-operator "fv-autolink").

### Does a reference variable actually contain the object?

No. It only contains the object's address. That's why two reference variables can point to the same single object, and why setting a reference to [null](/ap-comp-sci-a/key-terms/null "fv-autolink") doesn't delete the object, it just disconnects that variable from it.

### What's the difference between a reference variable and a primitive variable?

A primitive variable like an int stores its value directly, so copies are independent. A reference variable stores an address, so copying it creates a second pointer to the same object. Primitives also can't be null, but references can.

### Why does == return false for two objects that look identical?

Because == compares the addresses stored in the reference variables, not the objects' contents. Two separately constructed objects live at different addresses even with the same data, so use .equals() to compare what's inside them.

## Structured Data

```json
{"@context":"https://schema.org","@graph":[{"@type":"LearningResource","@id":"https://fiveable.me/ap-comp-sci-a/key-terms/reference-variable#resource","name":"Reference Variable — AP Comp Sci A Definition & Exam Guide","url":"https://fiveable.me/ap-comp-sci-a/key-terms/reference-variable","learningResourceType":"Concept explainer","educationalLevel":"AP® / High School","about":{"@id":"https://fiveable.me/ap-comp-sci-a/key-terms/reference-variable#term"},"audience":{"@type":"EducationalAudience","educationalRole":"student"},"isPartOf":{"@type":"Collection","name":"AP Computer Science A Key Terms","url":"https://fiveable.me/ap-comp-sci-a/key-terms"},"publisher":{"@type":"Organization","name":"Fiveable","url":"https://fiveable.me"}},{"@type":"DefinedTerm","@id":"https://fiveable.me/ap-comp-sci-a/key-terms/reference-variable#term","name":"Reference Variable","description":"In AP Computer Science A, a reference variable is a variable that stores the memory address of an object rather than the object itself, letting you access that object's methods and instance variables; it's how all non-primitive (reference) types work in Java.","url":"https://fiveable.me/ap-comp-sci-a/key-terms/reference-variable","inDefinedTermSet":{"@type":"DefinedTermSet","name":"AP Computer Science A Key Terms","url":"https://fiveable.me/ap-comp-sci-a/key-terms"}},{"@type":"FAQPage","mainEntity":[{"@type":"Question","name":"What is a reference variable in AP Computer Science A?","acceptedAnswer":{"@type":"Answer","text":"It's a variable that stores the memory address of an object instead of the object itself. In `Dog d = new Dog();`, the object lives in memory and `d` just holds directions to it, which you follow using the [dot operator](/ap-comp-sci-a/key-terms/dot-operator \"fv-autolink\")."}},{"@type":"Question","name":"Does a reference variable actually contain the object?","acceptedAnswer":{"@type":"Answer","text":"No. It only contains the object's address. That's why two reference variables can point to the same single object, and why setting a reference to [null](/ap-comp-sci-a/key-terms/null \"fv-autolink\") doesn't delete the object, it just disconnects that variable from it."}},{"@type":"Question","name":"What's the difference between a reference variable and a primitive variable?","acceptedAnswer":{"@type":"Answer","text":"A primitive variable like an int stores its value directly, so copies are independent. A reference variable stores an address, so copying it creates a second pointer to the same object. Primitives also can't be null, but references can."}},{"@type":"Question","name":"Why does == return false for two objects that look identical?","acceptedAnswer":{"@type":"Answer","text":"Because == compares the addresses stored in the reference variables, not the objects' contents. Two separately constructed objects live at different addresses even with the same data, so use .equals() to compare what's inside them."}}]},{"@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"AP Computer Science A","item":"https://fiveable.me/ap-comp-sci-a"},{"@type":"ListItem","position":2,"name":"Key Terms","item":"https://fiveable.me/ap-comp-sci-a/key-terms"},{"@type":"ListItem","position":3,"name":"Reference Variable"}]}]}
```
