Variables & Naming ๐ฆ¶
Mentor's Note: A variable is just a Labeled Box in your computer's memory. In Java, you must be very clear about two things: What is the name of the box? and What kind of things can it hold? ๐ก
๐ The Scenario: The Storage Room ๐ฆ¶
Imagine you are organizing a massive storage room.
- The Box (The Memory): You have thousands of empty boxes. To find anything, you need to Label them. ๐ฆ
- The Label (The Variable Name): You put a label on a box:
studentAge. ๐ท๏ธ - The Type (The Box Shape): In Java, boxes come in specific shapes. You can't put a liquid (a String) in a box made for solid blocks (an Integer). ๐
- The Initialization (The Content): You put the value
20inside thestudentAgebox. ๐ - The Result: Now, whenever you need to know the age, you just look for the box labeled
studentAge! โ
๐จ Visual Logic: The Variable Structure¶
| Component | Rule | Example |
|---|---|---|
| Data Type | What can it hold? | int (Whole Numbers) |
| Name | How do we find it? | userScore (camelCase) |
| Value | What is inside? | 100 |
graph LR
A[Data Type: int ๐งฑ] --> B[Name: age ๐ท๏ธ]
B --> C[Assignment: = โก๏ธ]
C --> D[Value: 25 ๐]
D --> E[Semicolon: ; ๐ง]
๐ Concept Explanation¶
1. Declaration & Initialization ๐๏ธ¶
- Declaration: Telling Java to "Reserve a box" (
int age;). - Initialization: Putting a value "In the box" (
age = 25;). - Together:
int age = 25;
2. The final Keyword (Constants) ๐¶
If you use final, the box is Locked after you put the first value inside. You cannot change it! Useful for things like PI = 3.14.
3. Naming Conventions (camelCase) ๐ช¶
In Java, we use camelCase.
- Correct: firstName, totalPrice, isUserLoggedIn.
- Incorrect: first_name, FirstName, firstname.
๐ป Implementation: The Storage Lab¶
// ๐ Scenario: Storing student data
// ๐ Action: Declaring different types of boxes
public class Main {
public static void main(String[] args) {
// ๐ฆ Labeled Boxes
String studentName = "Vishnu";
int rollNumber = 101;
double percentage = 95.5;
boolean isPassed = true;
// ๐ A Locked Box (Constant)
final int MAX_SCORE = 100;
// MAX_SCORE = 150; // โ ERROR: Cannot change final variable
// ๐ท๏ธ Multiple Boxes in one line
int x = 5, y = 10, z = 15;
System.out.println("Student: " + studentName);
System.out.println("Roll No: " + rollNumber);
}
}
๐ Sample Dry Run (Logic)¶
| Step | Instruction | Computer's Logic | Result |
|---|---|---|---|
| 1 | int score; |
Reserve a small numeric box | Name 'score' is ready ๐๏ธ |
| 2 | score = 50; |
Put '50' inside the box | score = 50 โ |
| 3 | score = 60; |
Replace '50' with '60' | score = 60 (Updated!) โ |
๐ Technical Analysis: Static Typing ๐ง ¶
Java is a Statically Typed language. This means once a box is labeled int, you can NEVER put a String (text) inside it.
- Java: "Once an integer, always an integer!" ๐ก๏ธ
- Benefit: This prevents 90% of bugs that happen in "Flexible" languages like Python or JavaScript.
๐ฏ Practice Lab ๐งช¶
Task: The Price Calculator
Task: Create a variable for price (double) and quantity (int). Calculate the total by multiplying them.
Goal: Use proper names and correct data types. ๐ก
๐ก Interview Tip ๐¶
"Interviewers often ask: 'Can we start a variable name with a number?' Answer: NO. Variable names must start with a letter,
$, or_. Numbers can be used, but not at the very beginning (e.g.,user1is okay,1useris not)."
๐ก Pro Tip: "Always use meaningful names. Instead of
int x = 20;, useint userAge = 20;. Your future self will thank you!" - Vishnu Damwala