Skip to content
Veltoryx Technologies
ProLesson 2.7Beginner2 min readUpdated 30 October 2026

Arrays

Store many values in one array, loop over it, sort and search it with the Arrays class, and use 2D arrays for grids.

An array stores a fixed number of values of the same type, one after another. Think of a row of numbered lockers: each locker has an index, starting at 0.

01Creating arrays

ArrayBasics.java
public class ArrayBasics {
    public static void main(String[] args) {
        int[] marks = {72, 88, 95, 61};        // values known up front
        String[] names = new String[3];          // 3 empty slots (null)
        names[0] = "Asha";
        names[1] = "Ravi";

        System.out.println(marks[0]);            // first element
        System.out.println(marks[marks.length - 1]); // last element
        System.out.println(marks.length);        // size
        System.out.println(names[2]);            // never set
    }
}
Expected output
72
61
4
null

New arrays are filled with default values: 0 for numbers, false for boolean, null for objects.

Loading the rest of the lesson…