Skip to content

Latest commit

 

History

History
1394 lines (747 loc) · 16.5 KB

File metadata and controls

1394 lines (747 loc) · 16.5 KB

Arrays Introduction in Java — DEEP DIVE 🔥

Let's build arrays from why they exist → memory concept → syntax → creation → indexing → initialization → loops → common errors → important interview doubts.


  1. What Problem Does an Array Solve?

Suppose you want to store the marks of 5 students.

Without an array:

int mark1 = 80; int mark2 = 75; int mark3 = 90; int mark4 = 65; int mark5 = 88;

This works, but imagine storing 10,000 marks.

You would need thousands of variables.

Instead:

int[] marks = {80, 75, 90, 65, 88};

Now one variable:

marks ↓ [80][75][90][65][88]

So the basic purpose of an array is:

Store multiple values of the same data type under one array reference and access them using indexes.


  1. What Exactly Is an Array?

A precise definition:

An array in Java is an object that contains a fixed number of elements of the same component type, accessed using a zero-based index.

There are several important words here.

Object

An array is an object in Java.

Fixed number

Once created, its length cannot be changed.

Same component type

For example:

int[] a;

contains int elements.

double[] b;

contains double elements.

Zero-based index

The first element is at index 0.


  1. Visualize an Array

Consider:

int[] marks = {80, 75, 90, 65, 88};

Think of it as:

Variable/reference | ↓ marks | ↓ ┌────┬────┬────┬────┬────┐ │ 80 │ 75 │ 90 │ 65 │ 88 │ └────┴────┴────┴────┴────┘ 0 1 2 3 4 indexes

Therefore:

marks[0] → 80 marks[1] → 75 marks[2] → 90 marks[3] → 65 marks[4] → 88


  1. The Most Important Array Rule

If:

int[] a = new int[5];

then:

length = 5

Valid indexes are:

0 1 2 3 4

Not:

5

Formula

First index = 0

Last index = length - 1

Therefore:

length = 5 last index = 5 - 1 = 4

This single formula prevents many array errors.


  1. Array Declaration

You can declare an array like this:

int[] numbers;

This means:

numbers is a variable capable of referring to an integer array.

At this point, no array object has been created.

Conceptually:

numbers ↓ null

The reference currently doesn't refer to an array object.


  1. Array Creation

Now create the actual array:

numbers = new int[5];

Combined:

int[] numbers = new int[5];

This creates space for:

5 integers

Conceptually:

numbers | ↓ [0][0][0][0][0]

Because int elements receive the default value 0.


  1. Declaration vs Creation vs Initialization

This is an important distinction.

Declaration

int[] numbers;

You declare the reference variable.

Creation

numbers = new int[5];

You create the array object with 5 elements.

Initialization

numbers[0] = 10; numbers[1] = 20;

You assign values.

Or you can do all three conveniently:

int[] numbers = {10, 20, 30};


  1. Three Common Ways to Create Arrays

Method 1 — Declaration + creation

int[] numbers = new int[5];

Initially:

[0][0][0][0][0]


Method 2 — Direct initialization

int[] numbers = {10, 20, 30, 40};

Java determines the size automatically.

length = 4


Method 3 — Explicit array creation with values

int[] numbers = new int[]{10, 20, 30, 40};

This is also valid.


  1. Why Does new int[5] Not Mean Index 5?

This is a classic beginner doubt.

int[] a = new int[5];

means:

Create 5 elements.

It does not mean:

Create indexes 0 through 5.

The indexes are:

0 1 2 3 4

Five positions.


  1. Accessing Array Elements

Use the index operator:

[]

Example:

int[] numbers = {10, 20, 30};

System.out.println(numbers[0]);

Output:

10

Why?

numbers ↓ [10][20][30] ↑ index 0


  1. Updating an Array Element

Array elements can be changed.

int[] numbers = {10, 20, 30};

numbers[1] = 50;

Before:

[10][20][30]

After:

[10][50][30]

Then:

System.out.println(numbers[1]);

prints:

50


  1. Can Array Size Change?

No ❌

Suppose:

int[] a = new int[5];

The array has exactly 5 elements.

You cannot turn that same array into a 10-element array.

You would need a new array:

a = new int[10];

This creates a new array object and makes a refer to it.

The old array is no longer referenced by a.


  1. Why Is Array Size Fixed?

An array represents a fixed-length structure.

If you need a dynamically growing/shrinking collection, Java provides collection classes such as:

ArrayList

But don't confuse them:

Array ↓ fixed length

ArrayList ↓ dynamic size


  1. The length Property

For arrays, use:

numbers.length

Example:

int[] numbers = {10, 20, 30, 40};

System.out.println(numbers.length);

Output:

4

Important:

length is a field/property, not a method.

Correct:

numbers.length

Incorrect:

numbers.length()


  1. Array length vs String length()

This causes frequent confusion.

Array:

int[] a = {10, 20, 30};

System.out.println(a.length);

String:

String s = "Java";

System.out.println(s.length());

So:

Array → length String → length()


  1. Array + for Loop

Arrays and loops are natural partners.

int[] numbers = {10, 20, 30, 40};

for (int i = 0; i < numbers.length; i++) { System.out.println(numbers[i]); }

Trace:

i = 0 numbers[0] → 10

i = 1 numbers[1] → 20

i = 2 numbers[2] → 30

i = 3 numbers[3] → 40

i = 4 4 < 4 → false STOP


  1. Why Do We Write i < numbers.length?

Suppose:

int[] numbers = new int[5];

Indexes:

0 1 2 3 4

Therefore:

i < 5

allows:

i = 0 i = 1 i = 2 i = 3 i = 4

But:

i <= 5

would eventually make:

i = 5

which is invalid.

Therefore the standard pattern is:

for (int i = 0; i < array.length; i++)


  1. Array + Enhanced for

Java provides an easier loop:

int[] numbers = {10, 20, 30, 40};

for (int n : numbers) { System.out.println(n); }

Read it as:

For each n in numbers.

Output:

10 20 30 40


  1. Normal for vs Enhanced for

Normal for

for (int i = 0; i < numbers.length; i++) { System.out.println(numbers[i]); }

Useful when you need the index.

Enhanced for

for (int n : numbers) { System.out.println(n); }

Useful when you simply need the elements.

Think:

Need index? ↓ normal for

Need only values? ↓ enhanced for


  1. Can Enhanced for Change the Array?

Be careful.

int[] numbers = {10, 20, 30};

for (int n : numbers) { n = 100; }

The array remains:

10 20 30

Why?

n is a loop variable receiving each element's value.

To modify array elements, use indexes:

for (int i = 0; i < numbers.length; i++) { numbers[i] = 100; }

Now:

100 100 100


  1. Default Values

When you create:

int[] a = new int[5];

Java automatically initializes the elements.

For primitive types:

Type Default value

byte 0 short 0 int 0 long 0L float 0.0f double 0.0d char '\u0000' boolean false

For reference types:

String[] names = new String[3];

initially:

null null null


  1. Why Does Java Give Default Values?

Because array elements are created as part of the array object.

For example:

int[] a = new int[3];

Java doesn't leave those integer slots containing random garbage values.

They start as:

0 0 0


  1. ArrayIndexOutOfBoundsException

Consider:

int[] a = {10, 20, 30};

Valid:

a[0] a[1] a[2]

Invalid:

a[3]

You'll get:

ArrayIndexOutOfBoundsException

Similarly:

a[-1]

is invalid.

Remember:

0 ≤ index < length

That is the mathematical rule for a valid array index.


  1. What Happens If the Array Reference Is null?

Consider:

int[] a = null;

System.out.println(a.length);

This results in:

NullPointerException

Why?

Because a doesn't refer to an array object.

Think:

a ↓ null

There is no array whose length can be accessed.


  1. Array Variables Store References

This is an important Java concept.

Consider:

int[] a = {10, 20, 30};

The variable a is a reference variable referring to an array object.

Conceptually:

a │ │ reference ↓ ┌────┬────┬────┐ │ 10 │ 20 │ 30 │ └────┴────┴────┘

This is why arrays behave like objects.


  1. Two References Can Refer to the Same Array

int[] a = {10, 20, 30};

int[] b = a;

Now:

┌─────────────┐ a ─────┤ │ ↓ │ [10][20][30] │ ↑ │ b ─────┘

Both references refer to the same array.

Therefore:

b[0] = 100;

Now:

System.out.println(a[0]);

prints:

100

Because a and b point to the same array object.


  1. Array Assignment Does Not Copy the Array

This:

int[] b = a;

does not mean:

copy all elements

It means:

copy the reference

If you need a separate copy, you can use mechanisms such as:

a.clone()

or:

Arrays.copyOf(...)


  1. Arrays Can Store Objects

Arrays aren't limited to primitive types.

String[] names = { "Ali", "Ravi", "John" };

Conceptually:

names ↓ ["Ali"]["Ravi"]["John"]

You can also create:

Student[] students = new Student[5];

The array contains references to Student objects.

Initially:

null null null null null

until objects are assigned.


  1. Important: Array Type and Element Type

Consider:

int[] a;

The array's component type is:

int

Consider:

String[] names;

The component type is:

String

Consider:

Student[] students;

The component type is:

Student


  1. Can an Array Store Different Primitive Types?

No.

This is invalid:

int[] a = {10, 20, 3.5};

because 3.5 is a double.

An int[] requires integer elements.

But Java's type system allows certain compatible conversions in array initializers, such as:

double[] a = {10, 20, 3.5};

because the integer literals can be represented as doubles.


  1. Is an Array a Primitive Data Type?

No ❌

This is important.

int double char boolean

are primitive types.

But:

int[] double[] String[]

are reference types, and array values are objects.


  1. Can an Array Have Length Zero?

Yes ✅

int[] a = new int[0];

This is a valid array.

Its:

length = 0

There are no valid element indexes.

It is different from:

int[] a = null;

Difference:

new int[0] ↓ actual array object ↓ length 0

null ↓ no array object


  1. Array of Arrays

Java supports multidimensional arrays.

For example:

int[][] matrix = new int[2][3];

Think:

columns 0 1 2 ┌───┬───┬───┐ 0 │ 0 │ 0 │ 0 │ ├───┼───┼───┤ 1 │ 0 │ 0 │ 0 │ └───┴───┴───┘ rows

Access:

matrix[0][1]


  1. Java's Multidimensional Arrays Are Special

Java doesn't technically require every row to have the same length.

You can create:

int[][] a = new int[2][];

a[0] = new int[3]; a[1] = new int[5];

So:

row 0 → [ ][ ][ ]

row 1 → [ ][ ][ ][ ][ ]

This is called a jagged/ragged array.

For now, remember:

A multidimensional Java array is essentially an array whose elements can themselves be arrays.


  1. Array of char vs String

You can have:

char[] letters = {'J', 'a', 'v', 'a'};

This is an array of characters.

But:

String word = "Java";

is a String object.

They are related concepts, but they are not the same type.


  1. Common Beginner Mistake #1

Mistake:

int[] a = new int[5];

for (int i = 0; i <= a.length; i++) { System.out.println(a[i]); }

Problem:

When:

i = 5

the program tries:

a[5]

But valid indexes are:

0–4

Correct:

for (int i = 0; i < a.length; i++) { System.out.println(a[i]); }


  1. Common Beginner Mistake #2

Mistake:

System.out.println(a.length());

Correct:

System.out.println(a.length);

Because array length is a field.


  1. Common Beginner Mistake #3

Mistake:

int[] a; a[0] = 10;

This doesn't create an array.

You declared the reference but didn't create an array object.

Correct:

int[] a = new int[5];

a[0] = 10;


  1. Common Beginner Mistake #4

Trying to resize:

int[] a = new int[5];

// "increase a to 10 elements"

You cannot resize that existing array.

Instead:

a = new int[10];

creates a new array.

If you need automatic resizing, consider ArrayList.


  1. A Complete Program

class ArrayDemo {

public static void main(String[] args) {

    int[] marks = {80, 75, 90, 65, 88};

    System.out.println("Number of elements: " + marks.length);

    System.out.println("First mark: " + marks[0]);
    System.out.println("Last mark: " + marks[marks.length - 1]);

    System.out.println("All marks:");

    for (int i = 0; i < marks.length; i++) {
        System.out.println(marks[i]);
    }
}

}

Output:

Number of elements: 5 First mark: 80 Last mark: 88 All marks: 80 75 90 65 88

Notice:

marks[marks.length - 1]

is the safest general way to access the last element.


  1. Array Initialization — Three Forms

Form 1

int[] a = {10, 20, 30};

Form 2

int[] a = new int[]{10, 20, 30};

Form 3

int[] a = new int[3];

a[0] = 10; a[1] = 20; a[2] = 30;

All create an integer array containing:

10 20 30


  1. A Very Important Syntax Rule

This is valid:

int[] a = {10, 20, 30};

But this is not valid as a standalone later assignment:

a = {10, 20, 30}; // ❌

Instead:

a = new int[]{10, 20, 30}; // ✅

The {...} shorthand is allowed directly in an array variable initializer.


  1. Array vs Normal Variables

Feature Normal variable Array

Values Usually one value Multiple elements Type One declared type One component type Index No Yes Size One value Fixed number of elements Example int x int[] x Access x x[index]


  1. Array vs ArrayList

Array ArrayList

Fixed size Dynamic size Can store primitives directly Stores objects/reference types length size() a[i] list.get(i) Usually simpler/faster for fixed-size data Convenient for changing-size collections

Example:

int[] a = new int[5];

vs.

ArrayList list = new ArrayList<>();

Don't confuse:

array.length

with:

ArrayList.size()


  1. The Deepest Mental Model

When you write:

int[] marks = {80, 90, 70};

think:

marks │ │ reference ↓ ┌────────┬────────┬────────┐ │ 80 │ 90 │ 70 │ └────────┴────────┴────────┘ [0] [1] [2]

Then:

marks[1]

means:

Go to the array referenced by marks and access element at index 1.

Result:

90

And:

marks.length

means:

Ask the array object how many elements it contains.

Result:

3


🔥 ARRAY DOUBT-KILLER

Is an array a primitive?

No. It's an object/reference type.

Is array size fixed?

Yes, after creation.

Does indexing start at 0?

Yes.

Last index?

length - 1

Can elements be changed?

Yes.

Can the array itself be resized?

No.

Can an array contain primitives?

Yes.

int[] a;

Can an array contain objects?

Yes.

String[] names;

Can an array contain different unrelated types?

Generally no; its component type determines what it can store.

Is length a method?

No.

a.length

Can an array have length 0?

Yes.

What happens with an invalid index?

ArrayIndexOutOfBoundsException

What happens when the reference is null?

Accessing it causes:

NullPointerException

Does int[] b = a copy the elements?

No. Both references refer to the same array.

Can Java have multidimensional arrays?

Yes.

int[][] matrix;


🧠 FINAL DEEP-DIVE MAP

ARRAY │ ┌─────────────┴─────────────┐ ↓ ↓ SAME TYPE FIXED SIZE │ │ └─────────────┬─────────────┘ ↓ INDEX BASED │ first index = 0 │ last = length - 1 │ ┌─────────────┼─────────────┐ ↓ ↓ ↓ access modify loop a[i] a[i]=x for / foreach │ ↓ a.length │ ↓ number of elements

🏆 The 5 rules you absolutely must remember

  1. Array stores multiple elements of a component type.

  2. Array indexing starts from 0.

  3. Last index = array.length - 1.

  4. Array length is fixed after creation.

  5. array.length gives the number of elements; it is not length().