Understanding data types and variables is the first real step into programming logic. Before building applications, you must know how Java stores and manages data.
This guide explains everything in a clear, practical, and interview-ready way.
🔹 What are Variables in Java?
A variable is a container used to store data in memory.
👉 Think of it like a labeled box:
- Label → Variable name
- Content → Value
🔸 Example:
int age = 25;
👉 Here:
int→ Data typeage→ Variable name25→ Value
🔹 Why Variables are Important
Variables allow you to:
- Store user input
- Perform calculations
- Manage application data
👉 Without variables, programming is impossible.
🔹 What are Data Types?
A data type defines:
- What type of value a variable can hold
- How much memory it uses
- What operations can be performed
🔹 Types of Data Types in Java
Java data types are mainly divided into:
1. Primitive Data Types
2. Non-Primitive (Reference) Data Types
🔹 1. Primitive Data Types
These are the basic building blocks of data.
| Type | Size | Example |
|---|---|---|
| byte | 1 byte | 100 |
| short | 2 bytes | 20000 |
| int | 4 bytes | 100000 |
| long | 8 bytes | 100000L |
| float | 4 bytes | 10.5f |
| double | 8 bytes | 20.99 |
| char | 2 bytes | 'A' |
| boolean | 1 bit | true/false |
🔸 Example:
int number = 100;
double price = 99.99;
char grade = 'A';
boolean isJavaFun = true;
👉 These store actual values directly in memory.
🔹 2. Non-Primitive (Reference) Data Types
These store references (addresses), not actual values.
Examples:
- String
- Arrays
- Classes
- Objects
🔸 Example:
String name = "Surendra";
👉 Here:
namestores reference to string object
🔹 Key Difference (Primitive vs Reference)
| Feature | Primitive | Reference |
|---|---|---|
| Stores | Value | Address |
| Memory | Fixed | Dynamic |
| Examples | int, char | String, Array |
🔹 Variable Declaration & Initialization
🔸 Declaration:
int age;
🔸 Initialization:
age = 25;
🔸 Combined:
int age = 25;
🔹 Types of Variables in Java
1. Local Variables
Declared inside methods.
void show() {
int x = 10;
}
2. Instance Variables
Declared inside class but outside methods.
class Student {
int age;
}
3. Static Variables
Shared across all objects.
class Demo {
static int count = 0;
}
🔹 Example Program (All Concepts Together)
public class DataTypesDemo {
static int count = 0; // static variable
int age = 25; // instance variable
public static void main(String[] args) {
int number = 100; // local variable
double price = 49.99;
String name = "Java";
System.out.println(number);
System.out.println(price);
System.out.println(name);
}
}
🔹 Type Casting in Java
Type casting means converting one data type into another.
🔸 1. Implicit Casting (Widening)
int a = 10;
double b = a;
👉 Safe conversion
🔸 2. Explicit Casting (Narrowing)
double x = 10.5;
int y = (int) x;
👉 Data loss possible
🔹 Default Values of Variables
| Type | Default Value |
|---|---|
| int | 0 |
| double | 0.0 |
| boolean | false |
| object | null |
👉 Applies to instance variables only.
🔹 Rules for Naming Variables
- Use meaningful names
- Use camelCase
- Cannot start with number
- Avoid keywords
✅ Good Examples:
int studentAge;
double accountBalance;
❌ Bad Examples:
int x;
double a1;
🔹 Common Beginner Mistakes
❌ Using wrong data type
❌ Forgetting initialization
❌ Confusing primitive vs reference
❌ Poor naming
👉 These can cause bugs and confusion.
🔹 Best Practices
✔ Choose correct data type
✔ Use meaningful variable names
✔ Initialize variables properly
✔ Keep code clean and readable
🔹 What to Learn Next
After this, move to:
- Operators in Java
- Control Statements
- Arrays & Strings
- OOP Concepts
🔹 Conclusion
Data types and variables are the building blocks of Java programming.
👉 Once you master this:
- You can store and manipulate data
- You’re ready for logic building
- You can write real programs
Remember:
Every complex application starts with simple variables.
Comments
Post a Comment