0
0
Javaprogramming~15 mins

Primitive vs reference storage in Java

Choose your learning style8 modes available
menu_bookIntroduction

We use primitive and reference storage to hold data in Java. Primitives store simple values directly, while references store addresses to objects.

When you want to store simple data like numbers or true/false values.
When you want to store complex data like a group of values or behaviors in objects.
When you want to understand how Java handles memory for different types of data.
When you want to avoid unexpected changes by knowing if you are copying values or references.
When debugging issues related to data changes or memory usage.
regular_expressionSyntax
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.

emoji_objectsExamples
line_end_arrow_notchChanging b does not affect a because primitives store values directly.
Java
int a = 10;
int b = a;
b = 20;
System.out.println(a); // prints 10
line_end_arrow_notchChanging s2 to a new string does not change s1 because strings are immutable and s2 now points to a new object.
Java
String s1 = new String("Hi");
String s2 = s1;
s2 = "Bye";
System.out.println(s1); // prints Hi
line_end_arrow_notchArrays are reference types, so changing arr2 changes arr1 because both refer to the same object.
Java
int[] arr1 = {1, 2, 3};
int[] arr2 = arr1;
arr2[0] = 10;
System.out.println(arr1[0]); // prints 10
line_end_arrow_notchChanging x after copying does not affect y because primitives are copied by value.
Java
int x = 5;
int y = x;
x = 7;
System.out.println(y); // prints 5
code_blocksSample Program

This program shows how changing a copied primitive does not affect the original, but changing a copied reference affects the original object.

Java
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();
    }
}
OutputSuccess
emoji_objectsImportant Notes
line_end_arrow_notch

Time complexity for copying primitives is O(1) because it copies the value directly.

line_end_arrow_notch

Copying references is also O(1) but both variables point to the same object, so changes affect both.

line_end_arrow_notch

A common mistake is thinking that copying a reference creates a new object; it does not.

line_end_arrow_notch

Use primitives for simple data and references for objects or collections.

list_alt_checkSummary

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.