Variables, Scope, Type Conversion, Arrays & Strings in Java

1️⃣ Variables in Java (Storage Units)
A variable is a named storage location in memory.
In Java, a variable is defined by five things:
Type + Name + Value + Scope + Lifetime
int x = 10;
int→ Data typex→ Variable name10→ ValueScope → Where it can be accessed
Lifetime → How long it exists in memory
Because Java is strongly typed, every variable must have a declared type.
2️⃣ Scope & Lifetime (VERY IMPORTANT FOR INTERVIEWS)
Scope Rule
👉 A variable exists only inside its block {}
{
int x = 10;
}
x = 20; // ❌ Compile-time error
Once the block ends, the variable is destroyed.
Nested Scope Rule
Inner scope CAN access outer variables
Outer scope CANNOT access inner variables
int a = 10;
{
int b = 20;
System.out.println(a); // ✔
}
System.out.println(b); // ❌
Variable Lifetime
Created → when block starts
Destroyed → when block ends
for (int i = 0; i < 3; i++) {
int y = -1;
}
👉 y is recreated and destroyed on every iteration.
3️⃣ Type Conversion & Casting
Java supports two types of conversions.
Widening Conversion (Automatic) ✅
Smaller type → Larger type
int → long → float → double
int a = 10;
double d = a; // ✔
✔ Safe
✔ No data loss
Narrowing Conversion (Manual Cast) ❌
Larger type → Smaller type
int x = (int) 10.5;
⚠️ Fractional part is lost (truncation)
Result:
10
4️⃣ Type Promotion (CONFUSING BUT CRITICAL)
Rule:
👉 byte, short and char are automatically promoted to int in expressions.
byte a = 50;
byte b = 2;
byte c = (byte)(a * b);
Why casting is required?
a * b→ becomesintJava does not assign
inttobyteautomatically
💡 This is a classic Java interview trap.
5️⃣ Arrays in Java (MOST USED)
What Is an Array?
👉 A collection of same-type values
👉 Stored contiguously in memory
int[] arr = new int[5];
Index starts from
0Size is fixed
arr[0] = 10;
Array Initialization
int[] days = {31, 28, 31, 30};
Runtime Safety 🔥
Java checks array bounds at runtime:
arr[10]; // ❌ ArrayIndexOutOfBoundsException
👉 Prevents memory corruption
👉 Makes Java safer than C/C++
6️⃣ Multidimensional Arrays
Java uses arrays of arrays, not true matrices.
int[][] matrix = new int[3][4];
Jagged (Irregular) Arrays
int[][] arr = new int[3][];
Each row can have a different size.
📌 Used for:
Sparse data
Memory optimization
7️⃣ var – Type Inference (Java 10+)
var x = 10; // int
var d = 10.5; // double
Type is inferred at compile time.
Rules:
✔ Only for local variables
✔ Must be initialized
❌ Not allowed for fields
❌ Not allowed for method parameters
❌ Cannot assign null
var x = null; // ❌
💡 var improves readability, not dynamic typing.
8️⃣ Strings (Introduction Only)
String s = "Java";
Important facts:
String is an object
Immutable
Stored in String Constant Pool
Rich API with many methods
📌 Covered later in depth:
String pool
Immutability
==vsequals()
🎓 Final Mentor Summary
Variables live only within their scope
Java automatically manages variable lifetime
Widening conversions are safe, narrowing needs casting
Type promotion can cause hidden bugs
Arrays are safe but fixed-size
varis compile-time inferenceStrings are objects, not primitives