Primitive Variables vs. Referenced Variables in Java

Referenced Variables & Their Types in Java?

When learning Java, one of the most important things to understand is how data is stored and accessed in memory. Java has two main categories of variables – primitive variables and referenced variables.

In this blog, we’ll dive deep into the referenced variables in Java and explore:

✅ What is a reference variable?
✅ Difference between primitive and reference variables
✅ What values are stored in a reference variable?
✅ Types of reference variables
✅ Who allocates memory and when?
✅ A program demonstrating all major types
✅ JVM internal view (stack vs. heap)

Let’s begin! 🚀


✅ What is a Reference Variable in Java?

A reference variable in Java does not store actual data.
Instead, it stores the memory address (reference) of an object located in the Heap memory.

📌 Example:

Student s = new Student();

Here:

  • Student is a class
  • new Student() creates an object in heap memory
  • s is a reference variable that points to that object

So, reference variables act like remote controls used to access objects.

Want to explore everything new in Java 8? Check the complete guide here: Top 16 Java 8 Key Features Every Developer Should Know


✅ Primitive vs Reference Variables

https://i.sstatic.net/cUQTU.png
FeaturePrimitive VariableReference Variable
StoresActual valueAddress/reference of object
MemoryStored in StackReference in Stack, but object in Heap
Types8 primitivesUnlimited (Classes, arrays, interfaces)
Default valueType-specific (e.g., 0)null
Examplesint, double, charString, Object, custom class

Example:

int x = 10;    // primitive
String s = "Java";  // reference

✅ What values are stored in a Reference Variable?

A reference variable stores a pointer to the object in the heap.

Possible stored values:

ValueMeaning
Valid reference (address)Object exists in heap
nullReference pointing to nothing
Invalid referenceCauses NullPointerException when accessed

✅ Types of Reference Variables in Java

Below are the commonly used reference variable categories in Java:

1️⃣ Local Reference Variable

  • Declared inside a method
  • Exists only during method execution
  • Stored in Stack
void show() {
    Student s = new Student();  // Local reference
}

2️⃣ Static Reference Variable

  • Declared with static keyword
  • Belongs to the Class, not object
  • Loaded in the method area when class loads
static Student schoolTopper = new Student();

3️⃣ Non-static / Instance Reference Variable

  • Declared inside a class but outside any method
  • Exists as long as the object exists
  • Stored in Heap
class Demo {
    Student s = new Student(); // Instance reference variable
}

4️⃣ Final Reference Variable

  • Reference cannot change once assigned
  • Object values inside can still change
final Student std = new Student();
// std = new Student();  ❌ Not allowed

5️⃣ Volatile Reference Variable

  • Used in multi-threading
  • Value always read from main memory
  • Ensures visibility among threads
volatile Student data;

6️⃣ Transient Reference Variable

  • Used during serialization
  • Prevents value from being saved into files
transient Address addr;

✔ Works only with object-level fields (instance variables)


✅ Who allocates memory for Reference Variables & When?

Memory PartAllocated ByWhen?
StackJVMVariable declaration
HeapJVM (via new)Object creation
Method AreaJVMClass loading

Diagram for clarity:

https://media.geeksforgeeks.org/wp-content/uploads/20250109113400155148/Stack-and-Heap-Memory-in-Java.png
https://blog.jamesdbloom.com/images_2013_11_17_17_56/JVM_Internal_Architecture.png

✅ Java Program Showing All Reference Variable Types + JVM Memory Flow

class Student {
    String name;
}

public class ReferenceDemo {

    // Static Reference Variable
    static Student staticRef = new Student();

    // Instance / Non-static Reference Variable
    Student instanceRef = new Student();

    void display() {
        // Local Reference Variable
        Student localRef = new Student();

        System.out.println("Local Reference: " + localRef);
    }

    public static void main(String[] args) {

        ReferenceDemo obj = new ReferenceDemo();

        System.out.println("Static Reference: " + staticRef);
        System.out.println("Instance Reference: " + obj.instanceRef);

        obj.display();
    }
}

🔍 JVM Memory Architecture Breakdown

VariableMemory LocationWhen Created
staticRefMethod AreaClass loading
instanceRefHeap (inside object)Object creation
localRefStackMethod call

📌 new Student() objects always go to Heap


✅ FAQ — Reference Variables in Java

🔹 Can a reference variable refer to multiple objects?

✅ Yes, by assigning a new object. The old one may be removed by the garbage collector.

🔹 Can a reference variable be null?

✅ Yes, default value is null.

🔹 Can reference variables point to primitive values?

❌ No, only objects.


✅ Final Thoughts

Reference variables are the backbone of OOP in Java, allowing objects to exist in the heap and be accessed flexibly across program execution. Mastering how they interact with JVM memory (Stack + Heap + Method Area) will make you a more confident Java developer.

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply