DEV Community

Cover image for πŸš€ Day 29 of My Automation Journey – Arrays (Full Guide + Tricky Questions)
bala d kaveri
bala d kaveri

Posted on

πŸš€ Day 29 of My Automation Journey – Arrays (Full Guide + Tricky Questions)


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};
Enter fullscreen mode Exit fullscreen mode

πŸ‘‰ Internally JVM converts:

int[] arr = new int[]{10, 20, 30};
Enter fullscreen mode Exit fullscreen mode

πŸ’‘ JVM:

  • Creates object in Heap memory
  • Stores reference in variable

πŸ”Ή 3. Proof – Array is an Object

System.out.println(arr.getClass());
Enter fullscreen mode Exit fullscreen mode

Output:

class [I
Enter fullscreen mode Exit fullscreen mode

πŸ”Ή 4. Array Creation

int[] arr1 = {10, 20, 60, 30};
int[] arr2 = new int[5];
Enter fullscreen mode Exit fullscreen mode

πŸ”Ή 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]);
Enter fullscreen mode Exit fullscreen mode

πŸ”Ή 8. Looping Through Array

Normal Loop:

for(int i = 0; i < arr.length; i++) {
    System.out.println(arr[i]);
}
Enter fullscreen mode Exit fullscreen mode

Enhanced Loop:

for(int num : arr) {
    System.out.println(num);
}
Enter fullscreen mode Exit fullscreen mode

πŸ”Ή 9. Multi-Dimensional Array

int[][] matrix = {
    {1, 2},
    {3, 4}
};
Enter fullscreen mode Exit fullscreen mode

πŸ”Ή PART B – PROGRAMS

βœ… Program 1 – Default Values

int[] arr = new int[3];
System.out.println(arr[0] + " " + arr[1] + " " + arr[2]);
Enter fullscreen mode Exit fullscreen mode

Output:

0 0 0
Enter fullscreen mode Exit fullscreen mode

❌ Program 2 – Index Error

int[] arr = new int[3];
System.out.println(arr[3]);
Enter fullscreen mode Exit fullscreen mode

Output:

ArrayIndexOutOfBoundsException
Enter fullscreen mode Exit fullscreen mode

❌ Program 3 – Length Modification

int[] arr = {1, 2, 3};
System.out.println(arr.length);
arr.length = 10;
Enter fullscreen mode Exit fullscreen mode

Output:

3
Compilation Error
Enter fullscreen mode Exit fullscreen mode

πŸ”₯ Program 4 – Reference Behavior

int[] a = {1, 2, 3};
int[] b = a;
b[0] = 100;
System.out.println(a[0]);
Enter fullscreen mode Exit fullscreen mode

Output:

100
Enter fullscreen mode Exit fullscreen mode

πŸ”₯ Program 5 – Comparing Arrays

int[] a = {1, 2, 3};
int[] b = {1, 2, 3};
System.out.println(a == b);
Enter fullscreen mode Exit fullscreen mode

Output:

false
Enter fullscreen mode Exit fullscreen mode

❌ Program 6 – Null Array

int[] arr = null;
System.out.println(arr.length);
Enter fullscreen mode Exit fullscreen mode

Output:

NullPointerException
Enter fullscreen mode Exit fullscreen mode

❌ Program 7 – Negative Size

int[] arr = new int[-5];
Enter fullscreen mode Exit fullscreen mode

Output:

NegativeArraySizeException
Enter fullscreen mode Exit fullscreen mode

πŸ”Ή 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]);
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

πŸ” 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];
Enter fullscreen mode Exit fullscreen mode

πŸ‘‰ Stored in different memory


Passing Array

void test(int[] arr) {
    arr[0] = 999;
}
Enter fullscreen mode Exit fullscreen mode

πŸ‘‰ Passed by reference


πŸ”Ή PART E – TRICKY QUESTIONS

❓ Q1

int[] arr = new int[3];
System.out.println(arr[0] + " " + arr[1] + " " + arr[2]);
Enter fullscreen mode Exit fullscreen mode

βœ” Output: 0 0 0
πŸ‘‰ Default values


❓ Q2

System.out.println(arr[3]);
Enter fullscreen mode Exit fullscreen mode

❌ Output: Exception
πŸ‘‰ Index out of range


❓ Q3

arr.length = 10;
Enter fullscreen mode Exit fullscreen mode

❌ Compilation error
πŸ‘‰ Length is final


❓ Q4

int[] b = a;
Enter fullscreen mode Exit fullscreen mode

βœ” Same reference


❓ Q5

System.out.println(a == b);
Enter fullscreen mode Exit fullscreen mode

❌ false
πŸ‘‰ Different objects


❓ Q6

int[] arr = null;
System.out.println(arr.length);
Enter fullscreen mode Exit fullscreen mode

❌ NullPointerException


❓ Q7

new int[-5];
Enter fullscreen mode Exit fullscreen mode

❌ NegativeArraySizeException


πŸ”Ή PART F – public static void main (DETAILED πŸ”₯)

public static void main(String[] args)
Enter fullscreen mode Exit fullscreen mode

πŸ” Deep Breakdown

πŸ”Ή public

πŸ‘‰ JVM must access it


πŸ”Ή static

πŸ‘‰ No object required

πŸ‘‰ JVM calls directly:

ClassName.main()
Enter fullscreen mode Exit fullscreen mode

πŸ”Ή void

πŸ‘‰ No return value


πŸ”Ή main

πŸ‘‰ Entry point


πŸ”Ή String[] args

πŸ‘‰ Command-line input

java Test hello
Enter fullscreen mode Exit fullscreen mode

πŸ‘‰ 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)