Arrays of Reference Types
What you'll learn: How to create arrays that hold objects (like String or custom classes) instead of primitives.
The Core Idea
So far, you've worked with arrays of primitives (int[], double[], etc.). But arrays can also hold reference types—objects! The most common example is an array of String objects.
When you declare an array of reference types, you're creating an array of "slots" that can each hold a reference (memory address) to an object. Initially, these slots don't contain actual objects—they contain null, which means "no object yet."
Syntax for Reference Type Arrays
String[] names = new String[5]; // Array of 5 String references
This creates an array with 5 slots. Each slot can hold a String object, but right now all 5 slots are null (the default initialization value for reference types).
To actually store String objects, you must assign them:
names[0] = "Alice";
names[1] = "Bob";
// names[2], names[3], names[4] are still null
The Two-Step Creation
Think of it like a parking lot analogy: new String[5] creates 5 parking spaces, but they're empty. You must then "park" actual String objects in those spaces by assigning them.
This is different from primitive arrays, where new int[5] immediately gives you 5 integers (all 0).
Watch Out for null
If you try to use an array element that's still null, you'll get a NullPointerException:
System.out.println(names[2].length()); // ERROR! names[2] is null
Key Takeaway: Arrays of reference types hold references to objects, not the objects themselves; all elements default to null until you assign actual objects to them.