Non-Primitive Data Types
Non-primitive data types (also called reference data types) are used to store complex data and references to objects rather than actual values. They are created using classes and provide powerful features like methods, inheritance, and polymorphism. This topic is essential for understanding OOP, memory management, and real-time Java applications.
What Are Non-Primitive Data Types?
- Also known as reference types
- Store memory address (reference) of data
- Can be user-defined or built-in
- Support methods and behaviors
Why it matters: Non-primitive types enable object-oriented programming in Java.
Types of Non-Primitive Data Types in Java
- String
- Arrays
- Classes
- Interfaces
- Wrapper Classes
- Collections (high-level usage)
1. String
What Is a String?
- Represents a sequence of characters
- Immutable in nature
- Stored in String Constant Pool
String name = "Java";
Why it matters: Used extensively for text handling.
Example Use Case: Usernames, messages, file paths.
2. Arrays
What Is an Array?
- Stores multiple values of the same data type
- Fixed size
- Stored in contiguous memory
int[] marks = {80, 90, 85};
Why it matters: Efficient storage and iteration of data.
3. Class
What Is a Class?
- Blueprint for creating objects
- Contains variables, methods, constructors
class Employee {
int id;
String name;
}
Why it matters: Foundation of object-oriented programming.
4. Object
What Is an Object?
- Instance of a class
- Created using new keyword
Employee emp = new Employee();
Why it matters: Objects represent real-world entities.
5. Interface
What Is an Interface?
- Collection of abstract methods
- Supports multiple inheritance
interface Payment {
void pay();
}
Why it matters: Achieves abstraction and loose coupling.
6. Wrapper Classes
What Are Wrapper Classes?
- Convert primitives into objects
- Located in java.lang package
| Primitive | Wrapper |
|---|---|
| int | Integer |
| double | Double |
| char | Character |
| boolean | Boolean |
Integer age = 30;
Why it matters: Required for collections and object-based APIs.
7. Collections (Brief Intro)
- Used to store dynamic groups of objects
- Examples: ArrayList, HashSet, HashMap
List<String> names = new ArrayList<>();
Why it matters: Preferred over arrays for flexibility.
Memory Representation (Key Difference)
- Primitive → Stores actual value
- Non-Primitive → Stores reference (address)
int x = 10; // value
String s = "Java"; // reference
Default Values
- Default value of non-primitive types = null
- Applies to instance and static variables only
String city; // default value = null
Primitive vs Non-Primitive (Comparison)
| Feature | Primitive | Non-Primitive |
|---|---|---|
| Stores | Value | Reference |
| Size | Fixed | Varies |
| Methods | ❌ No | ✅ Yes |
| Null Allowed | ❌ No | ✅ Yes |
| OOP Support | ❌ No | ✅ Yes |
Common Mistakes by Beginners
- Assuming String is primitive
- Forgetting to initialize objects
- NullPointerException due to null reference
- Using arrays instead of collections unnecessarily
- Confusing class and object
Interview-Ready Answers
Short Answer
Non-primitive data types store references to objects and include String, arrays, classes, interfaces, and wrapper classes.
Detailed Answer
Non-primitive data types in Java are reference types that store the memory address of objects. They support object-oriented features such as methods, inheritance, and polymorphism, making them essential for real-world application development.
Key Takeaway
Non-primitive data types enable object-oriented programming in Java. They allow Java programs to model real-world entities, manage complex data, and build scalable applications.