Reference Guide

Key Topics

Term Definition Example
Array A fixed block of memory holding values of one type (the garage). int[] listOne = new int[5];
Element One value in the array (a car in a spot). listOne[0] = 5;
Index Position of an element, starting at 0 (the spot number). listOne[2]
Length How many elements the array holds; public final, so it can’t change. listOne.length is 5
Default value What Java puts in every slot when you use new without values. new int[5] → [0, 0, 0, 0, 0]
Initializer list Creates and fills the array in one step. int[] listTwo = {1, 2, 3, 4, 5};
Enhanced for loop Visits each value without giving you the index. for (int t : tickets)
  • length is public (anyone can read it) and final (locked at creation).
  • The last valid index is always length - 1.
  • System.out.println(arr) prints a memory reference like [I@28a6ceb, not the values.

Default Values

Type Default Value Notes
int 0  
double 0.0  
boolean false  
char '\u0000' Outside AP scope
String and any object null No object exists until you create one

Picking a Loop

  • Need the index, or need to change values in the array → standard for loop
  • Only need to read each value → enhanced for loop
  • Stop condition isn’t just “reached the end” → while loop
  • Finding a min or max → any loop, starting your “best so far” at the first element

College Board Requirements

AP CSA Unit 4, Topic 4.3 Array Creation and Access. Summarized from the course and exam description (College Board, 2025, p. 109):

  • Arrays are created with new and a size, or with an initializer list. Arrays created with new hold default values: 0, 0.0, false, or null.
  • Elements are accessed with square brackets and an index from 0 to length - 1. Using an index outside that range throws an ArrayIndexOutOfBoundsException.
  • Arrays can store primitive values or object references. Each object in an array of objects must be created separately.

The College Board says char is outside the scope of the AP Computer Science A course and exam. The default values table still lists it because it shows up in real code, but it will not be on the exam.

Lesson Plan

Learning Objective: Create arrays of primitives and objects, access and change their elements, and traverse them to find a minimum or maximum.

Success Criteria: You can predict default values, fill an array of objects without a NullPointerException, choose the right loop, and find the smallest or largest value.

Tech Talk

Picture a parking garage under construction. The contractor is told to build exactly 5 spots. Once the concrete is poured, a sixth spot doesn’t exist unless the whole garage is torn down and rebuilt bigger. That is new int[5].

Pouring the concrete doesn’t park any cars. Java fills every spot with a default value, because “empty” isn’t something a computer can store. Parking a car is assignment: listOne[0] = 5;.

Code Runner Challenge

Run it, then change new int[5] to new int[7] and predict which lines change

View IPYNB Source
// CODE_RUNNER: Run it, then change new int[5] to new int[7] and predict which lines change
public class GarageBasics {
    public static void main(String[] args) {
        int[] listOne = new int[5];        // [0, 0, 0, 0, 0]

        listOne[2] = 33;                    // [0, 0, 33, 0, 0]
        listOne[3] = listOne[2] + 3;        // [0, 0, 33, 36, 0]

        try {
            listOne[5] = 13;                // no such spot
        } catch (ArrayIndexOutOfBoundsException e) {
            System.out.println("Error at listOne[5] = 13: that spot was never built!");
        }

        System.out.println(listOne);        // the garage's address, not its contents
        System.out.println(listOne[3]);     // actually checks spot 3
    }
}

GarageBasics.main(null);
Lines: 1 Characters: 0
Output
Click "Run" in code control panel to see output ...

Code Examples

A. Default values. The GarageDefaults class builds four empty garages and checks a spot in each

Three ways to get values in get mixed up a lot:

Code Runner Challenge

Run it, then predict what spot 1 holds in each garage after you add plates[1] = "7ABC123";

View IPYNB Source
int[] a = new int[5];          // creation: 5 spots, all 0
a[0] = 5;                      // assignment: park one car
int[] b = {1, 2, 3, 4, 5};     // initializer list: build the garage pre-loaded
Lines: 1 Characters: 0
Output
Click "Run" in code control panel to see output ...

Code Runner Challenge

Run it, then delete the line that parks a car in spot 1 and see what the loop prints

View IPYNB Source
// CODE_RUNNER: Run it, then predict what spot 1 holds in each garage after you add plates[1] = "7ABC123";
public class GarageDefaults {
    public static void main(String[] args) {
        int[] tickets = new int[3];
        double[] fees = new double[3];
        boolean[] paid = new boolean[3];
        String[] plates = new String[3];

        System.out.println("tickets[1]: " + tickets[1]);
        System.out.println("fees[1]: " + fees[1]);
        System.out.println("paid[1]: " + paid[1]);
        System.out.println("plates[1]: " + plates[1]);
    }
}

GarageDefaults.main(null);
Lines: 1 Characters: 0
Output
Click "Run" in code control panel to see output ...

Terminology:

Primitive arrays fill with a real value (0, 0.0, false). Object arrays fill with null, meaning “no object here yet.”

B. Arrays of objects. The GarageObjects class shows empty spots before any cars are parked

Code Runner Challenge

Run it, then change earliestTicket to find the highest ticket instead

View IPYNB Source
// CODE_RUNNER: Run it, then delete the line that parks a car in spot 1 and see what the loop prints
public class GarageObjects {
    static class Car {
        private String plate;
        private int ticket;

        public Car(String plate, int ticket) {
            this.plate = plate;
            this.ticket = ticket;
        }

        public String getPlate() {
            return plate;
        }

        public String toString() {
            return plate + " (ticket #" + ticket + ")";
        }
    }

    public static void main(String[] args) {
        Car[] level1 = new Car[3];
        System.out.println("Spot 1 before parking: " + level1[1]);

        try {
            System.out.println(level1[1].getPlate());
        } catch (NullPointerException e) {
            System.out.println("NullPointerException: no car in spot 1 yet");
        }

        level1[0] = new Car("7ABC123", 102);
        level1[1] = new Car("8XYZ789", 97);
        level1[2] = new Car("5LMN456", 118);

        int i = 0;
        while (i < level1.length) {
            System.out.println("Spot " + i + ": " + level1[i]);
            i++;
        }
    }
}

GarageObjects.main(null);
Lines: 1 Characters: 0
Output
Click "Run" in code control panel to see output ...

Common object array mistake:

Code Runner Challenge

Replace the sample values with your own, then run it

View IPYNB Source
Car[] level1 = new Car[3];   // 3 empty spots, 0 cars
level1[0].getPlate();        // NullPointerException: nothing to ask
Lines: 1 Characters: 0
Output
Click "Run" in code control panel to see output ...

Terminology:

Printing null is fine. Calling a method on null is what throws the NullPointerException.

C. Enhanced for loops and min/max. The GarageWalkthrough class shows the copy rule and finds the longest-parked car

Code Runner Challenge

Fill in the blanks, then run it

View IPYNB Source
// CODE_RUNNER: Run it, then change earliestTicket to find the highest ticket instead
public class GarageWalkthrough {
    public static int earliestTicket(int[] ticketNumbers) {
        int lowest = ticketNumbers[0];   // assume the first car is the winner

        for (int i = 1; i < ticketNumbers.length; i++) {
            if (ticketNumbers[i] < lowest) {
                lowest = ticketNumbers[i];
            }
        }
        return lowest;
    }

    public static void main(String[] args) {
        int[] tickets = {102, 118, 97, 145, 103};

        for (int t : tickets) {
            t = 0;                        // changes the copy only
        }
        System.out.println("After enhanced for, tickets[0] = " + tickets[0]);

        for (int t : tickets) {
            System.out.println("Ticket: " + t);
        }

        System.out.println("Earliest ticket: " + earliestTicket(tickets));
    }
}

GarageWalkthrough.main(null);
Lines: 1 Characters: 0
Output
Click "Run" in code control panel to see output ...

Terminology:

The enhanced for loop variable is a copy of each value. Changing it is like scribbling on your own clipboard without repainting the number on the ground. Use an indexed loop to change the array.

Hacks & Practice Tasks

Popcorn Hack

2-minute challenge: you’re the new garage attendant on your first shift.

  • Build a 5-spot int garage with new
  • Park cars (ticket numbers) in 3 of the spots
  • Walk every spot with a standard for loop and call out its ticket number
  • Build a String array of 3 driver names, fill only 2
  • Walk the drivers with a while loop and print each one

Replace the sample values with your own.

// CODE_RUNNER: Replace the sample values with your own, then run it
public class AttendantShift {
    public static void main(String[] args) {
        // Try writing your own code here first!

        // Sample answer:
        int[] spots = new int[5];
        spots[0] = 102;
        spots[2] = 118;
        spots[4] = 97;

        for (int i = 0; i < spots.length; i++) {
            System.out.println("Spot " + i + ": " + spots[i]);
        }

        String[] drivers = new String[3];
        drivers[0] = "Bob";
        drivers[1] = "John";

        int i = 0;
        while (i < drivers.length) {
            System.out.println("Driver " + i + ": " + drivers[i]);
            i++;
        }
    }
}

AttendantShift.main(null);

Stretch: swap the String array for a class you built this year (nest it inside as a static class, like Car in Example B).

MCQ Check

6 questions, one at a time. Answer, check, then go to the next one. At the end, copy the score line into your notes.

MCQ Check
6 questions. Pick an answer to see feedback.
Question 1
double[] fees = new double[3];

What is fees[1]?

Question 2
String[] names = new String[2];
System.out.println(names[0]);

What is printed?

Question 3

After Car[] lot = new Car[4]; runs, how many Car objects exist?

Question 4
int[] t = {5, 10, 15};
for (int x : t) {
    x = x * 2;
}
System.out.println(t[1]);

What is printed?

Question 5

Using the same t, what does System.out.println(t); print?

Question 6

You need to add a $1.00 service charge to every fee in double[] fees. Which loop works?

Homework Hack

Task: Find the longest-parked car. Store the garage name in a final variable, five ticket numbers in an int array, and five fees in a double array. Find the earliest (smallest) ticket with a standard for loop, find the highest fee with an enhanced for loop, then add a $1.00 service charge to every fee using the loop that can actually change the array.

Solution Skeleton:

// CODE_RUNNER: Fill in the blanks, then run it
public class LongestParked {
    public static void main(String[] args) {
        final String GARAGE_NAME = "Your Garage Name";

        // 1. Ticket numbers and fees for 5 parked cars
        int[] tickets = {0, 0, 0, 0, 0};
        double[] fees = {0.0, 0.0, 0.0, 0.0, 0.0};

        // 2. Earliest (smallest) ticket with a standard for loop
        int earliest = tickets[0];

        // 3. Highest fee with an enhanced for loop
        double highestFee = fees[0];

        // 4. Add a 1.00 service charge to every fee (which loop can change the array?)

        System.out.println(GARAGE_NAME);
        System.out.println("Earliest ticket: " + earliest);
        System.out.println("Highest fee: " + highestFee);
        System.out.println("Fee at spot 0 after service charge: " + fees[0]);
    }
}

LongestParked.main(null);

References

College Board. (2025). AP Computer Science A course and exam description [Effective fall 2025].

Hoffman, B., et al. (2025). 4.3 Array creation and access. In CSAwesome2: AP CSA Java 2026+. Runestone Academy.

Hoffman, B., et al. (2025). 4.4 Array traversals. In CSAwesome2: AP CSA Java 2026+. Runestone Academy.

Oracle. (n.d.). Arrays. The Java Tutorials.

Qian, Y., & Lehman, J. (2017). Students’ misconceptions and other difficulties in introductory programming: A literature review. ACM Transactions on Computing Education, 18(1), Article 1.

All Java lessons