
Todayβs learning was one of the most important milestones in my journey π₯
π Arrays (core concept)
π JVM internal behaviour
π Memory understanding
π Tricky interview questions
π Deep dive into public static void main
This is where coding becomes real engineering π
πΉ PART A β ARRAYS COMPLETE THEORY
πΉ 1. What is an Array?
π An array is an object
π It is a container that:
β Stores multiple values
β Same data type
β Fixed size
πΉ 2. What Happens Internally (JVM π₯)
int[] arr = {10, 20, 30};
π Internally JVM converts:
int[] arr = new int[]{10, 20, 30};
π‘ JVM:
- Creates object in Heap memory
- Stores reference in variable
πΉ 3. Proof β Array is an Object
System.out.println(arr.getClass());
Output:
class [I
πΉ 4. Array Creation
int[] arr1 = {10, 20, 60, 30};
int[] arr2 = new int[5];
πΉ 5. Important Points β οΈ
β Fixed size
β Index starts from 0
β Stored in Heap
β Default values assigned
πΉ 6. Default Values
| Type | Default |
|---|---|
| int | 0 |
| boolean | false |
| object | null |
πΉ 7. Initialization & Access
arr[0] = 10;
System.out.println(arr[0]);
πΉ 8. Looping Through Array
Normal Loop:
for(int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
Enhanced Loop:
for(int num : arr) {
System.out.println(num);
}
πΉ 9. Multi-Dimensional Array
int[][] matrix = {
{1, 2},
{3, 4}
};
πΉ PART B β PROGRAMS
β Program 1 β Default Values
int[] arr = new int[3];
System.out.println(arr[0] + " " + arr[1] + " " + arr[2]);
Output:
0 0 0
β Program 2 β Index Error
int[] arr = new int[3];
System.out.println(arr[3]);
Output:
ArrayIndexOutOfBoundsException
β Program 3 β Length Modification
int[] arr = {1, 2, 3};
System.out.println(arr.length);
arr.length = 10;
Output:
3
Compilation Error
π₯ Program 4 β Reference Behavior
int[] a = {1, 2, 3};
int[] b = a;
b[0] = 100;
System.out.println(a[0]);
Output:
100
π₯ Program 5 β Comparing Arrays
int[] a = {1, 2, 3};
int[] b = {1, 2, 3};
System.out.println(a == b);
Output:
false
β Program 6 β Null Array
int[] arr = null;
System.out.println(arr.length);
Output:
NullPointerException
β Program 7 β Negative Size
int[] arr = new int[-5];
Output:
NegativeArraySizeException
πΉ PART C β PRACTICE PROGRAM
package modul4;
import java.util.Scanner;
public class ArrayPractise {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter array Length: ");
int count = sc.nextInt();
byte[] by = new byte[count];
short[] sh = new short[count];
System.out.println("Byte ");
for (int i = 0; i < count; i++) {
System.out.println(by[i]);
}
System.out.println("Short ");
for (int i = 0; i < count; i++) {
System.out.println(sh[i]);
}
}
}
π Output Insight
π Prints 0 because of default values
πΉ PART D β MEMORY & ADVANCED CONCEPTS
Separate Memory
int[] a = new int[3];
int[] b = new int[3];
π Stored in different memory
Passing Array
void test(int[] arr) {
arr[0] = 999;
}
π Passed by reference
πΉ PART E β TRICKY QUESTIONS
β Q1
int[] arr = new int[3];
System.out.println(arr[0] + " " + arr[1] + " " + arr[2]);
β Output: 0 0 0
π Default values
β Q2
System.out.println(arr[3]);
β Output: Exception
π Index out of range
β Q3
arr.length = 10;
β Compilation error
π Length is final
β Q4
int[] b = a;
β Same reference
β Q5
System.out.println(a == b);
β false
π Different objects
β Q6
int[] arr = null;
System.out.println(arr.length);
β NullPointerException
β Q7
new int[-5];
β NegativeArraySizeException
πΉ PART F β public static void main (DETAILED π₯)
public static void main(String[] args)
π Deep Breakdown
πΉ public
π JVM must access it
πΉ static
π No object required
π JVM calls directly:
ClassName.main()
πΉ void
π No return value
πΉ main
π Entry point
πΉ String[] args
π Command-line input
java Test hello
π args = ["hello"]
π‘ Important Insight
π Without main() β program wonβt run
π FINAL TAKEAWAYS
β Array = Object
β JVM handles memory
β Default values exist
β Reference behavior matters
β Exceptions are important
β main() is entry point
π‘ GOLDEN INSIGHT
π Arrays are the foundation of all data structures π
Stay tuned π
π€ A Small Note
I used ChatGPT to help structure and refine this blog while keeping the concepts aligned with my learning.
Top comments (0)