📘 DAY 16 JAVA NOTES – ARRAYS (From
Transcript)
1. Variable as a Named Block of Memory
A variable is a named block of memory.
Example:
int a = 10;
Here:
Memory is created
Value 10 is stored
Name of memory block is a
Important Point
A variable can store only one value at a time
If you assign again:
a = 20;
Old value 10 is replaced
Only 20 remains
Limitation
You cannot store multiple values (10, 20, 30…) in a single variable
2. Why Do We Need Arrays?
Requirement: store multiple values
Normal variables ❌ cannot do this
Arrays ✅ can store multiple values
Reason
Internal memory structure of arrays is different
Arrays use multiple memory blocks together
3. What Is an Array?
An array is a collection of multiple memory blocks
All blocks are:
Of same data type
Stored together
The entire array has one unique address
Example representation:
Index: 0 1 2 3 4
Value: 10 20 30 40 50
4. Index in Arrays
Each block has an index number
Index always starts from 0
No negative index exists ❌
Last index = size - 1
Example:
Size = 5
Indexes = 0, 1, 2, 3, 4
5. Array Reference Variable
Normal Variable
int a;
Stores actual value
Array Reference Variable
int[] a;
Stores address of array
Does NOT store data directly
Hence called reference variable
Key Difference
Normal Variable Array Reference Variable
Stores value Stores address
Single value Multiple values via array
6. Why It Is Called Reference Variable
Arrays store data in heap memory
Variable stores only the address
Address is called a reference
Just like:
Scanner sc = new Scanner([Link]);
sc stores address of Scanner object
Same concept applies to arrays.
7. Creating an Array (Using new )
Syntax
datatype[] arrayName = new datatype[size];
Example:
int[] a = new int[5];
What happens internally:
JVM creates array object at runtime
Memory blocks created
Default values assigned
Address stored in a
8. Default Values in Array
When array is created, JVM assigns default values:
Data Type Default Value
int 0
double 0.0
boolean false
String null
9. Length of an Array
Use length variable
It is not a method
[Link]
Important:
length → array (variable)
length() → String (method)
10. Initializing Array Values Using Index
You cannot assign directly:
a = 10; ❌
Correct way:
a[0] = 10;
a[1] = 20;
a[2] = 30;
Each value must be assigned using index
Index represents position
11. Why Direct Assignment Is Not Allowed
a stores an address
Not ready to store individual data
Hence:
a = 10; ❌ Compile-time error
12. Two Ways to Create an Array
1️⃣ Using new Keyword
int[] a = new int[5];
Size is mandatory
Default values assigned
2️⃣ Using Curly Braces
int[] a = {10, 20, 30, 40};
Size automatically decided
Compiler internally adds new
13. Array Is Fixed Size
Once created, size cannot change
Cannot grow or shrink
This is a drawback of arrays
Hence later we use Collections
14. Taking Input into Array Using Loop
Since:
Same statement repeats
Only index changes
Use for loop:
for(int i = 0; i < [Link]; i++) {
a[i] = [Link]();
}
15. Printing Array Elements
for(int i = 0; i < [Link]; i++) {
[Link](a[i] + " ");
}
16. Fetching and Modifying Array Values
Fetch value:
a[i]
Modify value:
a[i] = a[i] + 5;
Example:
Before: 10 20 30 40
After: 15 25 35 45
17. Returning an Array from a Method
Method Return Type
public static int[] methodName()
Return Statement
return arrayName;
Return type must match array type
Returned array must be stored in reference variable
18. Passing Array to a Method
public static void add(int[] a)
Method receives array reference
Any change inside method affects original array
19. Updating Array Inside Method
for(int i = 0; i < [Link]; i++) {
a[i] = a[i] + 5;
}
Values updated in same array
No new array required
20. Summary of What You Learned
✔ What is a variable
✔ Why arrays are needed
✔ Array structure and index
✔ Array reference variable
✔ Creating array (2 ways)
✔ Default values
✔ Array length
✔ Initializing array
✔ Loop usage with array
✔ Fetching and updating values
✔ Returning array from method
✔ Passing array to method