Java Programming Lesson 2 | Variables & Data Types Explained
LESSON - 2 VERIABLES AND DATA TYPES
Welcome to Lesson-2: Variables & Data Types 👨💻✨
📘 Lesson 2 — Variables & Data Types
✅ What is a Variable?
A variable is a container that stores data.
Example:
int age = 20; → age stores number 20
📌 Format:
✅ Java Data Types
Java has 2 categories:
| Category | Types | Examples |
|---|---|---|
| Primitive | 8 types | int, float, char, boolean, long, double, byte, short |
| Non-Primitive | String, Array, Class | "Hello", {1,2,3} |
✅ Primitive Data Types (Most Used)
| Type | Size | Example | Use |
|---|---|---|---|
| int | 4 bytes | 10 | Whole numbers |
| float | 4 bytes | 5.5f | Decimal (less accurate) |
| double | 8 bytes | 8.99 | Decimal (high accuracy) |
| char | 2 bytes | 'A' | Single character |
| boolean | 1 bit | true/false | Conditions |
| long | 8 bytes | 99999999L | Big whole numbers |
📌 Always remember:
float → add f
long → add L
✅ Practical Example
✅ Output:
✅ Naming Rules (Important Interview Question)
✔ Allowed: letters, numbers, _, $
✔ Must NOT start with a number
✔ Case-sensitive (age ≠ Age)
✔ No spaces
✅ Good:
❌ Bad:
✅ Mini Exercise (Do it Now ✅)
Write a program using variables to print:
📌 Your Name
📌 Your Age
📌 Your Country
📌 Your Favorite Programming Language
Example Output:
👉 Reply here with your code & output
I will check and improve it ✅
🎯 Quick Quiz
1️⃣ Which is a non-primitive data type?
A) int
B) String
C) boolean
2️⃣ Which is correct?
A) float x = 5.5;
B) float x = 5.5f;
Reply like: 1 → ?, 2 → ?
✅ Lesson-2 Completed!
Tell me when you're ready for:
➡ Lesson-3: Operators (Arithmetic, Logical, Relational)
Waiting for your answers + exercise 👇😊


No comments
Thank You For Visit My Website.
I Will Contact As Soon As Possible.