100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Java

Arrays in Java

Learn how Java arrays store fixed-size, homogeneous collections of elements on the heap, including declaration, default values, and common runtime errors.

Arrays & StringsBeginner9 min readJul 7, 2026
Analogies

1. Introduction

An array in Java is a container object that holds a fixed number of values of a single type. Once created, the size of an array cannot change, and every element must be the same declared type — this is why arrays are described as fixed-size and homogeneous.

🏏

Cricket analogy: A cricket array is like a printed 11-player scorecard slate - fixed at 11 slots the moment it's handed to the umpire, and every slot must hold a batsman's name, never a coach's.

Even though arrays hold primitive values or object references, in Java arrays are themselves objects. They are created on the heap using the 'new' keyword (or an array literal), they have a public final field called length, and their default value is null when declared as a reference without initialization.

🏏

Cricket analogy: A Team[] array is itself an object sitting in the heap like a franchise's official roster binder, created with new or listed directly, carrying a public final length field showing squad size, and defaulting to null if the binder was never issued.

2. Syntax

java
// Declaration and allocation
int[] arr = new int[5];        // preferred style
int arr2[] = new int[5];       // C-style, also legal

// Declaration with initialization (array literal)
int[] nums = {10, 20, 30, 40, 50};

// Declaring then assigning
int[] scores;
scores = new int[]{1, 2, 3};

3. Explanation

When you write 'new int[5]', Java allocates space for 5 integers on the heap and initializes every slot to that type's default value: 0 for numeric types, 0.0 for float/double, false for boolean, '\u0000' for char, and null for any reference/object type array. Arrays are zero-indexed, so valid indices for a length-5 array run from 0 to 4.

🏏

Cricket analogy: new int[5] is like reserving 5 blank scorecard boxes before the toss, each auto-filled with a default 0 runs, boolean fields defaulting to false ("not out"), and numbered box 0 through box 4 for the top five batsmen.

Accessing an index outside the valid range (negative, or >= length) does not silently fail — it throws an ArrayIndexOutOfBoundsException at runtime, since Java performs bounds checking on every array access.

🏏

Cricket analogy: Trying to read batsman index 11 on an 11-player scorecard (indices 0-10) throws an ArrayIndexOutOfBoundsException at runtime, just as the umpire would reject a 12th name on a locked lineup - Java checks every access.

Classic exam trap: arr.length is a FIELD (no parentheses), while String's length() is a METHOD. Writing arr.length() is a compile-time error, and writing str.length is also a compile-time error. Mixing these up is one of the most common beginner mistakes.

Because arrays are objects, an array variable actually holds a reference to the array on the heap. Assigning one array variable to another (b = a) copies the reference, not the elements — both variables then point to the same underlying array.

4. Example

java
public class ArrayDemo {
    public static void main(String[] args) {
        int[] scores = new int[4];   // default values: 0,0,0,0
        System.out.println("Default: " + scores[0]);

        int[] marks = {85, 90, 78, 92};
        System.out.println("Length: " + marks.length);

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

        try {
            System.out.println(marks[10]);
        } catch (ArrayIndexOutOfBoundsException e) {
            System.out.println("Caught: " + e.getMessage());
        }
    }
}

5. Output

text
Default: 0
Length: 4
marks[0] = 85
marks[1] = 90
marks[2] = 78
marks[3] = 92
Caught: Index 10 out of bounds for length 4

6. Key Takeaways

  • Arrays in Java have a fixed size determined at creation time and cannot be resized.
  • Arrays are objects stored on the heap and expose a public final field 'length' (not a method).
  • Numeric elements default to 0, boolean to false, and object references to null on creation.
  • Array indices are zero-based; out-of-range access throws ArrayIndexOutOfBoundsException.
  • Array variables hold references — copying the variable does not copy the underlying data.
  • Use 'new type[size]' or array-literal syntax '{v1, v2, ...}' to create arrays.

Practice what you learned

Was this page helpful?

Topics covered

#Java#JavaProgrammingStudyNotes#Programming#ArraysInJava#Arrays#Syntax#Explanation#Example#DataStructures#StudyNotes#SkillVeris