βœ… FULL DETAILED NOTES ON JAVA VARIABLES

(Beginner-friendly, expert-backed explanations)

πŸ“Œ Table of Contents

1. 🧠 What Are Variables?

A variable is a named container for storing data. You can think of it like a labeled box in your memory where you can keep values and use them throughout your program.

πŸ” β€œVariable” comes from β€œvary” β€” meaning the value it holds can change over time.

2. ✏️ Variables in Java vs. Math

In Math:

A = 10
B = 20
Area = A * B

In Java:

int a = 10;
int b = 20;
int area = a * b;

Java variables are just like in math β€” they hold values, like numbers or words β€” but Java requires you to specify a type.

3. πŸ›  Declaring a Variable in Java

Syntax:

<type> <variableName> = <value>;

Example:

int a = 5;
String name = "Tony Stark";
Part Description
int Data type (integer)
a Variable name (identifier)
= Assignment operator
5 Value stored in variable

πŸ’‘ The semicolon (;) ends the statement β€” mandatory in Java.

4. 🧱 Java's Memory Model: How Variables Work Under the Hood

Java uses RAM (memory) to store data in small blocks.

Each variable reserves a block of memory.

That block has:

Example:

int c = 25;

➑️ Java reserves memory block labeled c, stores 25, and marks it as type int.

🧠 Analogy: Think of memory like a city with houses. Each house has:

5. πŸ”– Identifiers: Naming Variables

An identifier is the name you give to variables, functions, classes, etc.

βœ… Rules for identifiers in Java:

6. πŸ”’ Types of Variables: Data Types in Java

Java is statically typed, so every variable must have a data type.

✴️ Common Data Types:

Data Type Description Example
int Whole numbers int age = 25;
double Decimal numbers double pi = 3.14;
char Single characters char grade = 'A';
String Sequence of characters String name = "Tony";
boolean true or false values boolean isActive = true;

7. ♻️ Changing Variable Values

int a = 10;
a = 50; // Now a holds 50

➑️ The old value is overwritten.

8. πŸ–¨ Printing Variable Values

System.out.println(a); // prints the value of a

➑️ The console will show the current value stored in a.

9. πŸ”„ Assigning One Variable to Another

int a = 10;
int b = 5;
a = b; // Now a = 5

➑️ The value of b is copied into a.

🧠 Important: Only the value is copied β€” not a reference (unless you're using objects).

10. 🧠 Expert Tips & Real-World Insight

final int maxLimit = 100;

11. πŸ“‹ Example Code (With Output)

public class VariableDemo {
    public static void main(String[] args) {
        int a = 10;
        int b = 5;
        int c = 25;
        String name = "Tony Stark";

        System.out.println(a);        // Output: 10
        System.out.println(b);        // Output: 5
        System.out.println(c);        // Output: 25
        System.out.println(name);     // Output: Tony Stark

        a = 50;
        System.out.println(a);        // Output: 50

        a = b;
        System.out.println(a);        // Output: 5
    }
}

12. πŸ“Œ Revision Notes

13. 🧠 Memory Tricks (to help you retain this)

14. πŸ’Ό Interview Questions + Model Answers

Q1: What is a variable in Java?

Answer:
A variable in Java is a named memory location used to store a value. The value can be changed during program execution. Variables must be declared with a type that tells the compiler what kind of data it will store (e.g., int, String, etc.). For example:

int score = 100;

Here, score is the variable name, storing an integer value 100.

Q2: What is the difference between int a = b; and int a == b;?

Answer:

Q3: How does Java manage memory when variables are created?

Answer:
When a variable is created, Java reserves a specific block of memory to store its value. The size of the memory depends on the variable's type (e.g., int uses 4 bytes). The variable name is a reference to that memory location, and each memory block has a unique address in RAM.

Q4: Can you reassign a value to a variable in Java?

Answer:
Yes. Variables in Java are mutable (unless declared final). You can change the value at any time after it's declared:

int age = 25;
age = 30; // age now holds 30

Q5: What are valid variable names in Java?

Answer:

Examples:

int userAge;  // valid
int _id;      // valid
int $salary;  // valid
int 1num;     // invalid