Skip to main content

Command Palette

Search for a command to run...

Primative and non primative

Published
•4 min read•View as Markdown

Java Memory & Data Types Explained Clearly

(Primitive, Non-Primitive, Objects, References, String, Arrays, Heap & Stack)

Understanding how Java handles data types and memory is one of the most important steps in becoming a good Java developer.
Most confusion in Java comes from not clearly understanding:

  • what is an object

  • what is a reference

  • where data is stored (stack vs heap)

This blog explains everything step by step, in simple language.


1. What Is a Data Type?

A data type tells Java:

  • what kind of data will be stored

  • how much memory is needed

  • what operations are allowed

Java data types are divided into two categories:

  1. Primitive data types

  2. Non-primitive data types


2. Primitive Data Types

Primitive data types store actual values directly.

Examples:

int x = 10;
double d = 5.5;
boolean flag = true;

Key properties of primitive types

  • Store value directly

  • Not objects

  • No methods

  • Cannot be null

  • Fixed size

  • Stored in stack memory

  • Very fast


3. Non-Primitive Data Types

Non-primitive data types are object-based.
They do not store values directly — they store references (addresses of objects).

Examples:

String s = "hi";
int[] arr = {1, 2, 3};
Student st = new Student();

Important rule

Variables are never objects. Objects are created separately and variables only hold references.


4. Object vs Reference (Very Important)

Example:

String s = "hi";
  • "hi" → object

  • s → reference variable

s is not an object.
It only points to the object.


5. Stack vs Heap (Core Concept)

Stack Memory

  • Stores primitive values

  • Stores reference variables

  • Method calls create stack frames

  • Automatically cleared when method ends

Heap Memory

  • Stores objects

  • Stores arrays

  • Managed by Garbage Collector

  • Slower than stack but flexible


6. String Literal vs new String()

Case 1: String Literal

String a = "hi";

What happens:

  • "hi" object is created in String Constant Pool (SCP)

  • a points to the SCP object

  • If "hi" already exists, no new object is created

String b = "hi";

a and b point to the same object


Case 2: Using new

String s = new String("hi");

What happens:

  1. JVM checks SCP for "hi"

    • If not present → SCP object may be created
  2. new String("hi") always creates a new object in heap

  3. s points to the heap object

Important:

Even if SCP object exists, s will NOT point to it.


7. Why SCP Exists

SCP exists to:

  • save memory

  • reuse string literals

  • improve performance

SCP is only for String literals.
No other class has this behavior.


8. Arrays in Java

Arrays are objects in Java.

Example:

int[] arr = {2, 4, 5};

Internally:

int[] arr = new int[]{2, 4, 5};
  • Array object → heap

  • arr → reference in stack


Another array example

int[] arr = new int[5];
arr[0] = 1;
arr[1] = 2;
arr[3] = 5;

Flow:

  1. Heap array created with default values:

     [0, 0, 0, 0, 0]
    
  2. Values updated:

     [1, 2, 0, 5, 0]
    

Unassigned indexes keep default value 0.


9. Execution Flow of Code

Example:

int[] arr = {2, 4, 5};

Execution flow:

  1. Reference variable arr created in stack

  2. Array object created in heap

  3. arr stores reference to heap object


10. What Happens When Program Ends?

When the program finishes:

  1. main() ends

  2. Stack frame is destroyed

    • All local variables removed
  3. Heap objects become unreachable

  4. Garbage Collector cleans memory

  5. JVM shuts down

  6. All memory is returned to OS

You never manually free memory in Java.


11. Universal Rules to Remember

  • Primitive → value → stack

  • Object → heap

  • Variable ≠ object

  • new → always heap

  • String literals → SCP

  • Arrays are objects

  • Java has references, not pointers

  • Java is memory safe


12. One-Line Interview Summary

Java uses primitive types for direct value storage and non-primitive types for object-based storage. Objects are created in heap memory, references are stored in stack memory, and strings use a special String Constant Pool for optimization.

More from this blog

codebhghvhv

34 posts