We use primitive and reference storage to hold data in Java. Primitives store simple values directly, while references store addresses to objects.
Primitive vs reference storage in Java
public class PrimitiveVsReference { public static void main(String[] args) { // Primitive type example int number = 5; // Reference type example String text = new String("Hello"); } }
Primitive types store actual values directly in memory.
Reference types store a reference (address) to the object in memory.
int a = 10; int b = a; b = 20; System.out.println(a); // prints 10
String s1 = new String("Hi"); String s2 = s1; s2 = "Bye"; System.out.println(s1); // prints Hi
int[] arr1 = {1, 2, 3}; int[] arr2 = arr1; arr2[0] = 10; System.out.println(arr1[0]); // prints 10
int x = 5; int y = x; x = 7; System.out.println(y); // prints 5
This program shows how changing a copied primitive does not affect the original, but changing a copied reference affects the original object.
public class PrimitiveVsReference { public static void main(String[] args) { // Primitive example int originalNumber = 100; int copiedNumber = originalNumber; copiedNumber = 200; System.out.println("Original number: " + originalNumber); System.out.println("Copied number: " + copiedNumber); // Reference example int[] originalArray = {1, 2, 3}; int[] copiedArray = originalArray; copiedArray[0] = 99; System.out.print("Original array after change: "); for (int num : originalArray) { System.out.print(num + " "); } System.out.println(); System.out.print("Copied array after change: "); for (int num : copiedArray) { System.out.print(num + " "); } System.out.println(); } }
Time complexity for copying primitives is O(1) because it copies the value directly.
Copying references is also O(1) but both variables point to the same object, so changes affect both.
A common mistake is thinking that copying a reference creates a new object; it does not.
Use primitives for simple data and references for objects or collections.
Primitives store actual values; references store addresses to objects.
Changing a copied primitive does not affect the original; changing a copied reference affects the original object.
Understanding this helps avoid bugs and manage memory better in Java.
Practice
Solution
Step 1: Understand primitive storage
Primitive types like int, double, and boolean store the actual data value directly in the variable's memory location.Step 2: Compare with reference types
Reference types store addresses pointing to objects, not the actual data value.Final Answer:
They store the actual value directly in the variable. -> Option BQuick Check:
Primitive = actual value [OK]
- Thinking primitives store memory addresses
- Confusing reference storage with primitive storage
- Assuming primitives store objects
Solution
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.Step 2: Verify type compatibility
int and boolean are primitive types and cannot be assigned a String object.Final Answer:
String s = new String(); -> Option DQuick Check:
Reference declaration matches object creation [OK]
- Assigning objects to primitive types
- Using incompatible types in declaration
- Missing 'new' keyword for object creation
int a = 5; int b = a; b = 10; System.out.println(a);
Solution
Step 1: Understand primitive assignment
Variable b copies the value of a (which is 5). Changing b to 10 does not affect a.Step 2: Print value of a
Since a was not changed, printing a outputs 5.Final Answer:
5 -> Option AQuick Check:
Primitive copy keeps original value [OK]
- Assuming a changes when b changes
- Confusing reference behavior with primitive
- Expecting compilation error
String s1 = "hello"; String s2 = s1; s2 = "world"; System.out.println(s1);
Solution
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.Step 2: Check effect on s1
s1 still points to "hello" because Strings are immutable and s2 reassignment does not affect s1.Final Answer:
No error; output is 'hello'. -> Option CQuick Check:
Reference reassignment doesn't change original object [OK]
- Thinking s1 changes when s2 changes
- Expecting compilation error for String assignment
- Confusing mutable and immutable objects
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);Solution
Step 1: Understand reference assignment for objects
b2 is assigned the reference of b1, so both point to the same Box object.Step 2: Modify object through b2
Changing b2.value to 10 changes the single Box object's value.Step 3: Print b1.value
Since b1 points to the same object, b1.value is now 10.Final Answer:
10 -> Option AQuick Check:
Reference change affects original object [OK]
- Assuming b1.value stays 5 after b2 changes it
- Confusing primitive and reference behavior
- Expecting compilation or runtime errors
Ready to prove it?
Pass the test and this pattern moves unknown
