AP CSA: Array References and Aliasing

AP CSA • Reference Semantics (Units 1 and 4) • Lesson Spotlight

Array References and Aliasing

An array variable holds an arrow, not the data. Copy the arrow and you get two names for one array. This is the reference model behind some of the most missed questions on the exam.

Design Code Develop Code Analyze Code Document Code Use Computers Responsibly
Loading the interactive lesson. If this note stays visible, this preview is blocking JavaScript. Download the file and open it in a browser (Chrome), or view it on the live page, to use the editor, game, and quiz.
Learn
Practice
Assess
Scenario

The problem this explains

Two people share one shopping list in a notes app. When either edits it, both see the change, because there is one list and two links to it. Copy the link and you still have one list; copy the list and you have two. Java arrays work exactly this way: a variable holds a reference (the link), and assigning it to another variable copies the link, not the list. Knowing which you have is the difference between a working FRQ and a silent bug.

Skill focus: today you Analyze code (predict what a shared reference does) and Develop code (write a real copy when you need one).
Learn

Two names, one array: references and aliasing

A primitive variable holds its value directly. A variable of an array (or object) type holds a reference: an arrow to where the real data lives on the heap. That one difference drives some of the most missed questions on the exam.

Primitives copy, references share

// primitive: b gets a COPY of the value
int x = 5;
int y = x;      // y is its own 5
y = 9;          // x is still 5

// reference: b gets a COPY of the arrow
int[] a = {1, 2, 3};
int[] b = a;    // same array!
b[0] = 9;       // a[0] is now 9 too
The rule
primitive = copies the value
array / object = copies the reference
result two names, one array

int[] b = a; does not build a second array. It copies the arrow, so a and b point at the same array. That shared state is called aliasing, and a change through either name is visible through the other.

== compares arrows, not contents

int[] a = {1, 2, 3};
int[] b = a;
int[] c = {1, 2, 3};

a == b   // true: same array
a == c   // false: different arrays,
         // even though contents match
Equality
a == b same object?
contents == does NOT check
Watch out: == on arrays and objects asks "are these the same object," not "do they hold equal values." Two different arrays with identical contents are == false. To compare contents you walk both with a loop (or use a content-equality method).

Making a real copy

To change one array without touching the other, build a new array and copy the elements in a loop. Now they are separate objects, so mutating one leaves the other alone.

Watch out: passing an array to a method passes the reference, so a method can change the caller's array. That is aliasing across a method boundary, and it is exactly what FRQ methods that modify an array rely on.

Vocabulary

Term Meaning
Reference The value an array or object variable holds: an arrow to data on the heap, not the data itself.
Aliasing Two or more references pointing at the same object, so a change through one is seen through all.
Heap The region of memory where arrays and objects live; variables on the stack reference into it.
Reference equality == asks whether two references point at the same object, not whether contents match.
Deep copy A new object with copied contents, independent of the original (built element by element for an array).

Predict first, then check

1. int[] a = {1, 2}; int[] b = a; b[0] = 7; What is a[0] now?
2. int[] a = {1}; int[] b = {1}; Is a == b true? Type 1 for true, 0 for false.
3. To copy an array so changes are NOT shared, you must build a ____ array. (one word)
See it in memory

See it in memory

Step through the aliasing scenario. Watch b copy the arrow (not the array), a mutation through b show up in a, and a fresh array become a separate object.

int[] a = {1, 2, 3};
int[] b = a;
b[0] = 9;
int[] c = {1, 2, 3};
Stack (variables)
Heap (objects)
Aliasing in memory: int[] b = a makes a and b point to the same array, so b[0] = 9 also changes a[0] to 9. A separately created array c with the same values is a different object, so a == c is false. Open in a browser to step through the animation.
Original interactive memory model. Stack variables hold arrows; the heap holds the arrays. Prev / Next / Play to step through.
Practice • write and run real Java

Live code editor

Eight problems, six scaffolded and two open-ended. Write Java, press Run to execute it, and Check to test against the expected output. Hints and the solution are here if you need them, but using them is tracked, so try first.

Practice • fluency

Alias or Copy?

Six quick rounds. Read the code and predict the result. For true/false, type 1 for true and 0 for false. This drills the aliasing traps the exam loves.

Assess • six questions at AP difficulty

Multiple choice

Predict your answer before reading the options. Watch for the qualifier words. After you answer, the rationale explains every distractor.

Assess • free-response connection

FRQ connection: methods share the reference

On the exam, passing an array to a method passes the reference, so the method can change the caller's array. This is aliasing across a method boundary. Practice the copy-then-modify pattern that keeps the original safe.

Every student challenged

Choose your lane

Support

  • Reread the Code and Memory panels
  • Editor problems 1 to 3 with hints on
  • Retry the quiz until 80%
  • Use the FRQ method shell

Core

  • All 8 editor problems
  • Full game run
  • The 6-question quiz once
  • The FRQ connection

Challenge

  • Open-ended problems 7 and 8 with no hints
  • Rewrite one loop a different way
  • Extend the FRQ
  • Lead a class trace
This lesson reports to the gradebook: lesson (completion), exercise-1 (code editor), exercise-2 (game), quiz (MCQ), and exercise-3 (FRQ). Live usage is tracked in window.LESSON_USAGE.

Get in Touch

Whether you're a student, parent, or teacher — I'd love to hear from you.

Just want free AP CS resources?

Enter your email below and check the subscribe box — no message needed. Students get daily practice questions and study tips. Teachers get curriculum resources and teaching strategies.

Typically responds within 24 hours

Message Sent!

Thanks for reaching out. I'll get back to you within 24 hours.

🏫 Welcome, fellow educator!

I offer curriculum resources, practice materials, and study guides designed for AP CS teachers. Let me know what you're looking for — whether it's classroom materials, a guest speaker, or Teachers Pay Teachers resources.

Email

[email protected]

📚

Courses

AP CSA, CSP, & Cybersecurity

Response Time

Within 24 hours

Prefer email? Reach me directly at [email protected]