Difference Between Primitive and Non-Primitive Datatypes
In Java, data types are broadly classified into Primitive and Non-Primitive (Reference) types.
Understanding the difference between them is important because they determine how data is stored, accessed, and manipulated in a Java program.
Memory Storage Comparison
| Feature | Primitive Datatypes | Non-Primitive Datatypes |
|---|---|---|
| Storage Location | Stored directly in stack memory | Reference (address) stored in stack, actual object in heap memory |
| Data Type Examples | int, float, char, boolean, byte, short, long, double | String, Array, Class, Interface, Enum |
| Size | Fixed (depends on type, e.g., int = 4 bytes) | Not fixed — depends on object size |
| Value Handling | Stores actual value | Stores reference (memory address) of the object |
| Default Value | Numeric → 0, Boolean → false, Char → '\u0000' | null |
Performance Differences
| Aspect | Primitive Types | Non-Primitive Types |
|---|---|---|
| Speed | Faster because they store simple values directly in stack memory | Slower due to object creation and heap memory access |
| Memory Efficiency | More memory-efficient | Requires more memory since they store object references |
| Operations | Support direct mathematical and logical operations | Need methods to perform operations (e.g., length(), toUpperCase()) |
| Null Values | Cannot be null (default values are assigned automatically) | Can be null, as they are objects |
| Garbage Collection | Not applicable | Managed by Java’s Garbage Collector |
Practical Examples
Example 1: Primitive Datatype
public class PrimitiveExample {
public static void main(String[] args) {
int num = 10;
System.out.println("Primitive Value: " + num);
}
}
Output:
Primitive Value: 10
Explanation:
- The variable num is a primitive int type.
- It directly stores the value 10 in the stack memory.
Example 2: Non-Primitive Datatype
public class NonPrimitiveExample {
public static void main(String[] args) {
String name = "ShikshaSanchar";
System.out.println("String Value: " + name);
System.out.println("Length: " + name.length());
}
}
Output:
String Value: ShikshaSanchar
Length: 14
Explanation:
- Here, name is a String object (non-primitive type).
- The variable stores a reference to the actual string object in heap memory.
- Methods like length() can be used to perform operations on it.
Summary Table:
| Criteria | Primitive | Non-Primitive |
|---|---|---|
| Type | Basic building blocks | Derived or reference-based |
| Stored Value | Actual data | Reference to data |
| Mutable? | Immutable (cannot hold null) | Can be mutable or immutable |
| Example | int x = 5; | String s = "Java"; |
| Memory Use | Lightweight | Heavier due to object references |
- Primitive datatypes are simple and store raw values directly.
- Non-primitive datatypes are objects that hold references to data in heap memory.
- Primitive types are faster and memory-efficient, while non-primitive types are more flexible and feature-rich.
- Java programs typically use both for balanced performance and functionality.