Curriculum / Java / Lesson 09

Arrays in Java

Java arrays store multiple values of the same type in a fixed-size, indexed structure — but unlike C, Java arrays are objects, and Java performs automatic bounds checking at runtime..

9 of 11
beginner 📖 5 min read 🎯 1 question

Arrays

Java arrays store multiple values of the same type in a fixed-size, indexed structure — but unlike C, Java arrays are objects, and Java performs automatic bounds checking at runtime.

💡 Key insight Accessing an out-of-range index in Java throws an ArrayIndexOutOfBoundsException instead of silently corrupting memory like in C.

Syntax

syntax
type[] arrayName = new type[size];
type[] arrayName = {value1, value2, ...};

Example

scores.length gives the array's size directly — no need for the C-style sizeof trick.

java
public class Main {
    public static void main(String[] args) {
        int[] scores = {90, 85, 70, 60, 95};
        for (int i = 0; i < scores.length; i++) {
            System.out.print(scores[i] + " ");
        }
    }
}
output90 85 70 60 95

Try it yourself

Copy this example into the Java compiler and run it — try changing a value and see what happens.

Quick Quiz

1. What exception does Java throw if you access an invalid array index?

Back to curriculum