Flip Every Card Before You Continue
Click or tap a card to flip it and reveal its definition and a code example. Turn on Quiz Me Mode to see the definition first and try to recall the term yourself before flipping!
Primitives vs. Reference Variables
Yesterday, you learned about primitives (like int, double, boolean). Primitives are simple values stored directly in Stack memory.
Today, we meet Reference Variables. Instead of holding raw data, a reference variable stores a memory address (pointer) that tells Java where to find the actual object in Heap memory.
The Memory Division
- Stores primitive variables
- Stores references (addresses)
- Super fast lookup
- Stores all objects (e.g., Strings, Dogs, Arrays)
- Dynamic sizing
- Objects persist longer
Interactive Guide
Step through the visualizer lines 1-4 to earn memory interaction points.
Creating Your Own Reference Variables
To create reference variables of a custom type, we first define a Class (the blueprint), then instantiate it using the new keyword.
1 The Blueprint (Custom Class)
public class Dog { String name; // instance variable // Constructor public Dog(String dName) { name = dName; } }
2 Instantiation Anatomy
Dog d1 = new Dog("Rex");
Dog: The class type (defines the reference type).
d1: Name of the reference variable stored on the Stack.
new: The magic operator that dynamically allocates memory on the Heap.
Dog("Rex"): Calls the constructor to initialize the actual data of the object on the Heap.
The "NullPointer" Pitfall!
If you declare a reference variable but don't allocate an object to it (e.g., Dog myDog;), its default value is null. Attempting to use a null reference triggers a NullPointerException crash!
Java Strings & Methods
Strings in Java are objects, meaning variables initialized to strings are actually references pointing to character arrays/sequences stored on the heap.
Run Methods Interactive
Extracts a portion. Crucial: Includes start index, EXCLUDES end index!
Splits the String around matches of the delimiter, returning an array of Strings.
String Indexed Memory Cells
Reference Types & String Mastery Quiz
1/6
Question Loading...
Quiz Complete!
You scored 0/0 (0%). Enter your name below to generate your official certificate.