Java Programming Lesson 2 | Variables & Data Types Explained
LESSON - 2 VERIABLES AND DATA TYPES
Understanding variables and data types is the foundation of Java programming. Without learning how to store, update, and use data, you cannot build real-world applications.
In this lesson, we will break down everything simply—perfect for beginners.
⭐ What Are Variables in Java?
A variable is like a container in memory where we store values.
✔ Example
Here:
intandStringare data typesageandnameare variables20and"John"are values
- Must start with a letter,
$, or_ - Cannot start with a number
- No spaces
- Case-sensitive (
ageandAgeare different)
✔ Good:
studentName, totalMarks, user_age
✘ Bad:
1age, user age, total-marks
⭐ Why Do We Use Variables?
Variables help programmers:
- Store data
- Process and manipulate information
- Make programs dynamic and interactive
- Reuse values anytime
Imagine creating a calculator, game, or website backend—variables make everything possible.
Java Data Types Explained
Java has two main categories of data types:
1. Primitive Data Types (Built-in Types)
Java provides 8 primitive types.
| Type | Size | Example | Meaning |
|---|---|---|---|
| byte | 1 byte | byte a = 10; | Small integers |
| short | 2 bytes | short s = 200; | Medium integers |
| int | 4 bytes | int age = 21; | Default integer type |
| long | 8 bytes | long views = 100000L; | Large integers |
| float | 4 bytes | float price = 10.5f; | Decimal numbers |
| double | 8 bytes | double salary = 50000.75; | High precision decimals |
| char | 2 bytes | char grade = 'A'; | Single characters |
| boolean | 1 bit | boolean isOnline = true; | True/false values |
📝 Example Program Using All Primitive Types
2. Non-Primitive (Reference) Data Types
These include:
- String
- Arrays
- Classes
- Objects
Differences from primitive types:
- Created by the programmer
- Can store multiple values
- Have methods and behaviors
⭐ String – Most Important Non-Primitive Type
Output:
How to Choose the Right Data Type
| Use Case | Best Data Type |
|---|---|
| Counting, age, number | int |
| Large number | long |
| Decimal number | float / double |
| A single letter | char |
| Yes/No value | boolean |
| Text or words | String |
Complete Beginner Example
Summary of Lesson 2
✔ Variables store information
✔ Java has 8 primitive data types
✔ Non-primitive types include String, Arrays, Objects
✔ Strings are most commonly used
✔ Naming variables correctly is important
✅ Naming Rules (Important Interview Question)
✔ Allowed: letters, numbers, _, $
✔ Must NOT start with a number
✔ Case-sensitive (age ≠ Age)
✔ No spaces
❌ 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 👇😊
▶ NEXT PART - Lesson-3: Operators
Frequently Asked Questions - (FAQs)
What is a variable in Java?
A variable in Java is a reserved memory location used to store data. Each variable has a name, a type, and a value that can change during program execution.
What are data types in Java?
Data types specify the kind of data a variable can store. Java supports primitive data types (int, double, char, boolean, etc.) and non-primitive types (String, arrays, classes).
What is the difference between primitive and non-primitive data types?
Primitive types store simple values directly in memory, while non-primitive types store references to objects. Primitive types are fast and memory-efficient, whereas non-primitive types provide more flexibility.
How do you declare a variable in Java?
You declare a variable using the format: dataType variableName = value;. For example: int age = 20;.
Can a variable value be changed in Java?
Yes. Variables declared without the final keyword can have their values updated anytime during runtime.
What is type casting in Java?
Type casting converts one data type into another, such as converting double to int. Java supports implicit (automatic) and explicit (manual) casting.
What is a String in Java?
A String is a non-primitive data type used to store sequences of characters. It is treated as an object and belongs to the Java String class.

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