Bird
Raised Fist0
Javaprogramming~10 mins

Primitive vs reference storage in Java - Visual Side-by-Side Comparison

Choose your learning style10 modes available
Concept Flow - Primitive vs reference storage
Declare primitive variable
↓
Store value directly in variable
↓
Declare reference variable
↓
Create object in heap memory
↓
Store reference (address) in variable
↓
Access object via reference
↓
Modify object data affects all references
Primitive variables hold actual values directly. Reference variables hold addresses pointing to objects in memory.
Execution Sample
Java
int a = 5;
int b = a;
b = 10;

String s1 = new String("Hi");
String s2 = s1;
s2 = "Hello";
Shows how primitives copy values and references copy addresses to objects.
Execution Table
StepActionVariable StatesMemoryNotes
1int a = 5;a=5Stack: a=5Primitive value 5 stored in 'a'
2int b = a;a=5, b=5Stack: a=5, b=5Value of 'a' copied to 'b'
3b = 10;a=5, b=10Stack: a=5, b=10'b' changed, 'a' unchanged
4String s1 = new String("Hi");s1->obj1Heap: obj1="Hi" Stack: s1->obj1Object created in heap, s1 points to it
5String s2 = s1;s1->obj1, s2->obj1Heap: obj1="Hi" Stack: s1->obj1, s2->obj1s2 references same object as s1
6s2 = "Hello";s1->obj1, s2->obj2Heap: obj1="Hi", obj2="Hello" Stack: s1->obj1, s2->obj2s2 now points to new object, s1 unchanged
7Enda=5, b=10, s1->obj1("Hi"), s2->obj2("Hello")Heap: obj1="Hi", obj2="Hello" Stack: a=5, b=10, s1->obj1, s2->obj2Execution finished
💡 All assignments done, variables hold expected values and references
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4After Step 5After Step 6Final
aundefined555555
bundefined51010101010
s1undefinedundefinedundefinedobj1obj1obj1obj1
s2undefinedundefinedundefinedundefinedobj1obj2obj2
Key Moments - 3 Insights
Why does changing 'b' not affect 'a' after copying primitive values?
Because primitives store actual values, 'b' gets its own copy. Changing 'b' does not change 'a' as shown in step 3 of the execution_table.
Why do 's1' and 's2' point to the same object after 's2 = s1;'?
Because reference variables store addresses, both 's1' and 's2' point to the same object in heap memory (step 5). Changing one reference does not copy the object.
What happens when 's2' is assigned a new string literal?
A new object is created and 's2' points to it (step 6). 's1' still points to the original object, so they are independent now.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3, what is the value of 'a'?
A10
B5
Cundefined
D0
💡 Hint
Check the 'Variable States' column at step 3 in execution_table
At which step do 's1' and 's2' stop pointing to the same object?
AStep 6
BStep 5
CStep 4
DStep 3
💡 Hint
Look at the 'Memory' column in execution_table for when s2 points to a new object
If we change 'b = 10;' to 'b = a + 5;', what would be the value of 'b' after step 3?
A5
B10
C15
Dundefined
💡 Hint
Recall that 'a' is 5 and 'b' is assigned 'a + 5' at step 3
Concept Snapshot
Primitive variables store actual values directly.
Reference variables store addresses pointing to objects in heap memory.
Assigning primitives copies values; assigning references copies addresses.
Changing a primitive copy does not affect original.
Changing a reference variable can point to a new object without affecting others.
Full Transcript
This lesson shows the difference between primitive and reference storage in Java. Primitive variables like 'int' hold actual values directly in memory. When you assign one primitive variable to another, the value is copied, so changing one does not affect the other. Reference variables like 'String' hold addresses pointing to objects stored in heap memory. Assigning one reference variable to another copies the address, so both point to the same object. Changing the reference to point to a new object does not affect the other references. The execution table traces these steps with variable states and memory changes, helping visualize how Java handles primitives and references.

Practice

(1/5)
1. Which of the following correctly describes how primitive types are stored in Java?
easy
A. They store values as strings internally.
B. They store the actual value directly in the variable.
C. They store the memory address of the variable.
D. They store a reference to an object in memory.

Solution

  1. Step 1: Understand primitive storage

    Primitive types like int, double, and boolean store the actual data value directly in the variable's memory location.
  2. Step 2: Compare with reference types

    Reference types store addresses pointing to objects, not the actual data value.
  3. Final Answer:

    They store the actual value directly in the variable. -> Option B
  4. Quick Check:

    Primitive = actual value [OK]
Hint: Primitives hold values; references hold addresses [OK]
Common Mistakes:
  • Thinking primitives store memory addresses
  • Confusing reference storage with primitive storage
  • Assuming primitives store objects
2. Which of the following is the correct way to declare a reference variable for a String in Java?
easy
A. int s = new String();
B. boolean s = new String();
C. String s = 10;
D. String s = new String();

Solution

  1. Step 1: Check variable type and assignment

    String is a reference type, so it can be assigned with new String() to create a new object.
  2. Step 2: Verify type compatibility

    int and boolean are primitive types and cannot be assigned a String object.
  3. Final Answer:

    String s = new String(); -> Option D
  4. Quick Check:

    Reference declaration matches object creation [OK]
Hint: Reference types use 'new' with matching type [OK]
Common Mistakes:
  • Assigning objects to primitive types
  • Using incompatible types in declaration
  • Missing 'new' keyword for object creation
3. What will be the output of the following code?
int a = 5;
int b = a;
b = 10;
System.out.println(a);
medium
A. 5
B. 10
C. 0
D. Compilation error

Solution

  1. Step 1: Understand primitive assignment

    Variable b copies the value of a (which is 5). Changing b to 10 does not affect a.
  2. Step 2: Print value of a

    Since a was not changed, printing a outputs 5.
  3. Final Answer:

    5 -> Option A
  4. Quick Check:

    Primitive copy keeps original value [OK]
Hint: Changing copied primitive doesn't affect original [OK]
Common Mistakes:
  • Assuming a changes when b changes
  • Confusing reference behavior with primitive
  • Expecting compilation error
4. Consider the code below. What is the error and how to fix it?
String s1 = "hello";
String s2 = s1;
s2 = "world";
System.out.println(s1);
medium
A. Output is 'world' because s2 changes s1.
B. Error: Strings cannot be assigned like this.
C. No error; output is 'hello'.
D. Compilation error due to missing new keyword.

Solution

  1. Step 1: Analyze reference assignment

    s2 points to the same String object as s1 initially, but assigning s2 = "world" makes s2 point to a new String object.
  2. Step 2: Check effect on s1

    s1 still points to "hello" because Strings are immutable and s2 reassignment does not affect s1.
  3. Final Answer:

    No error; output is 'hello'. -> Option C
  4. Quick Check:

    Reference reassignment doesn't change original object [OK]
Hint: Reassigning reference doesn't change original object [OK]
Common Mistakes:
  • Thinking s1 changes when s2 changes
  • Expecting compilation error for String assignment
  • Confusing mutable and immutable objects
5. Given the following code, what will be the output?
class Box {
  int value;
  Box(int v) { value = v; }
}

Box b1 = new Box(5);
Box b2 = b1;
b2.value = 10;
System.out.println(b1.value);
hard
A. 10
B. 5
C. Compilation error
D. NullPointerException

Solution

  1. Step 1: Understand reference assignment for objects

    b2 is assigned the reference of b1, so both point to the same Box object.
  2. Step 2: Modify object through b2

    Changing b2.value to 10 changes the single Box object's value.
  3. Step 3: Print b1.value

    Since b1 points to the same object, b1.value is now 10.
  4. Final Answer:

    10 -> Option A
  5. Quick Check:

    Reference change affects original object [OK]
Hint: Object references share same data; change affects all [OK]
Common Mistakes:
  • Assuming b1.value stays 5 after b2 changes it
  • Confusing primitive and reference behavior
  • Expecting compilation or runtime errors
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard