Java Tutorial #5 – Data Types in Java (Primitive vs Non-Primitive)
๐ Learn Java Data Types from basics to interview level in Hinglish, with fun examples and deep insights. Perfect for beginners and kids learning Java!
๐ฌ What is a Data Type?
Data Type batata hai kis type ka data store karna hai. Jaise school mein roll number number mein likhte hain aur name letters mein — waise hi Java ko bhi data ka type batana padta hai.
int age = 12;
String name = "Amit";
๐ก Why Data Types Are Needed
Data types help Java understand how much memory to allocate and what kind of operations can be performed. Without it, Java won’t know whether to reserve 2 bytes or 8 bytes.
๐ข Primitive Data Types
| Type | Size | Example | Description |
|---|---|---|---|
| byte | 1 byte | byte b = 10; | Very small integer |
| short | 2 bytes | short s = 1000; | Small integer |
| int | 4 bytes | int i = 100000; | Default integer |
| long | 8 bytes | long l = 100000L; | Large number |
| float | 4 bytes | float f = 10.5f; | Decimal (less precise) |
| double | 8 bytes | double d = 10.5; | Decimal (more precise) |
| char | 2 bytes | char grade = 'A'; | Single character |
| boolean | 1 bit | boolean isOn = true; | True/False value |
๐ง Memory Concept: Stack vs Heap
int a = 10; // stored directly in Stack
String name = "Amit"; // reference in Stack, actual object in Heap
Stack Memory: Fast and stores primitive data and references.
Heap Memory: Large and stores actual objects.
๐งฉ Analogy: Stack is like the canteen counter — where order slips (references) are placed, and Heap is the kitchen — where the actual food (data) is prepared!
๐งฉ Non-Primitive Data Types
String school = "CodeTulika School";
int[] marks = {90, 80, 85};
class Student {
int age;
String name;
}
Primitive types store actual values; non-primitive store references to objects.
๐ง Deep Concepts (For Interviews)
Wrapper Classes
int x = 5;
Integer y = x; // Autoboxing
int z = y; // Unboxing
Immutable String
String name = "Amit";
name.concat(" Kumar");
System.out.println(name); // Still "Amit"
Strings are immutable — once created, their value can’t be changed. Any change creates a new object.
๐งพ Recap
- Data Types tell what kind of data to store.
- Primitive → 8 types, store values directly.
- Non-Primitive → Objects, store references in Heap.
- Stack vs Heap difference is crucial for interviews.
- Wrapper Classes & Immutable Strings – advanced must-know topics.
๐ Next Episode: Operators in Java – perform magic with your data (+, -, &&, || and more!)
Created with ❤️ by CodeTulika
Comments
Post a Comment