Non-Primitive Data Types in Java – Complete Master Guide
Java programming language me data types do category me divide hote hain: Primitive Data Types aur Non-Primitive Data Types. Is article me hum sirf Non-Primitive Data Types ko deeply samjhenge — concept, memory structure, OOP connection, examples aur interview level tak.
What are Non-Primitive Data Types?
Non-Primitive Data Types ko Reference Types bhi kaha jata hai. Ye actual value store nahi karte, balki memory address store karte hain. Ye objects ke form me exist karte hain.
Simple Definition
Non-Primitive data type wo hota hai jo ek object ka reference store karta hai.
Main Types of Non-Primitive Data Types
- String
- Array
- Class
- Object
- Interface
- Collection Framework Classes
1️⃣ String in Java
String ek class hai jo text data ko represent karti hai. Ye immutable hoti hai, matlab ek baar value assign ho gayi to change nahi hoti.
String name = "Kamal";
System.out.println(name);
Why String is Non-Primitive?
- Ye class hai
- Iske methods hote hain
- Reference store karta hai
Common String Methods
name.length();
name.toUpperCase();
name.toLowerCase();
name.charAt(0);
2️⃣ Array in Java
Array ek collection hota hai jo same data type ke elements ko store karta hai. Array bhi object hota hai.
int[] numbers = {10,20,30,40};
System.out.println(numbers[2]);
Memory Concept
Array heap memory me store hota hai aur variable uska reference rakhta hai.
Advantages of Array
- Multiple values store kar sakte hain
- Fast access using index
- Structured data storage
3️⃣ Class in Java
Class ek blueprint hoti hai object create karne ke liye.
class Student {
String name;
int age;
}
Why Class is Non-Primitive?
Kyuki class ka object banaya jata hai aur reference store hota hai.
4️⃣ Object in Java
Student s1 = new Student();
Yaha s1 object ka reference hai. Actual object heap memory me store hota hai.
Heap vs Stack Memory
| Stack | Heap |
|---|---|
| Reference store karta hai | Actual object store karta hai |
| Fast memory | Large memory area |
5️⃣ Interface in Java
Interface abstraction provide karta hai. Ye pure abstraction ka concept implement karta hai.
interface Animal {
void sound();
}
Primitive vs Non-Primitive Comparison
| Primitive | Non-Primitive |
|---|---|
| Stores actual value | Stores reference |
| Fixed size | No fixed size |
| No methods | Methods available |
Real Life Example
Primitive ko samjho ek single number ki tarah. Non-Primitive ko samjho ek file folder ki tarah jisme multiple data ho sakta hai.
Interview Questions
Q1: Why String is non-primitive?
Because it is a class and stores reference.
Q2: Where are non-primitive types stored?
Heap memory.
Q3: Can we create object without class?
No, class blueprint hoti hai.
Advantages of Non-Primitive Data Types
- Support OOP
- Dynamic memory allocation
- Complex data handling
- Code reusability
Common Mistakes Beginners Make
- String ko primitive samajhna
- Array indexing error
- Null reference error
Conclusion
Non-Primitive Data Types Java ka backbone hain. Ye OOP concept ko implement karte hain. Agar aap Java developer banna chahte ho to inhe deeply samajhna zaruri hai.
Related Posts: Primitive Data Types | Variables & Constants
0 टिप्पणियाँ