Java Episode 5 – Data Types in Java (Primitive vs Non-Primitive) | CodeTulika

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

TypeSizeExampleDescription
byte1 bytebyte b = 10;Very small integer
short2 bytesshort s = 1000;Small integer
int4 bytesint i = 100000;Default integer
long8 byteslong l = 100000L;Large number
float4 bytesfloat f = 10.5f;Decimal (less precise)
double8 bytesdouble d = 10.5;Decimal (more precise)
char2 byteschar grade = 'A';Single character
boolean1 bitboolean 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

Popular posts from this blog

Java Episode 3 – Understanding JDK, JRE & JVM in Simple Desi Style

๐Ÿ’ป How to Download and Install Java on Mac and Ubuntu – Step-by-Step Guide