0
0
Javaprogramming~15 mins

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

Choose your learning style8 modes available
flowchartConcept 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.
code_blocksExecution 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.
data_tableExecution 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
search_insightsVariable Tracker
VariableStartAfter Step 2After Step 3After Step 4After Step 5After Step 6Final
aundefined555555
bundefined51010101010
s1undefinedundefinedundefinedobj1obj1obj1obj1
s2undefinedundefinedundefinedundefinedobj1obj2obj2
keyKey Moments - 3 Insights
Why does changing 'b' not affect 'a' after copying primitive values?
Why do 's1' and 's2' point to the same object after 's2 = s1;'?
What happens when 's2' is assigned a new string literal?
psychologyVisual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3, what is the value of 'a'?
A10
B5
Cundefined
D0
photo_cameraConcept 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.
contractFull 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.