Primative and non primative
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:
Primitive data types
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
nullFixed 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"→ objects→ 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)apoints to the SCP objectIf
"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:
JVM checks SCP for
"hi"- If not present → SCP object may be created
new String("hi")always creates a new object in heapspoints to the heap object
Important:
Even if SCP object exists,
swill 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:
Heap array created with default values:
[0, 0, 0, 0, 0]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:
Reference variable
arrcreated in stackArray object created in heap
arrstores reference to heap object
10. What Happens When Program Ends?
When the program finishes:
main()endsStack frame is destroyed
- All local variables removed
Heap objects become unreachable
Garbage Collector cleans memory
JVM shuts down
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 heapString 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.