Java Arrays

</>Coding Tamilan.← Java course & curriculum
Java Masterclass · Lesson 18 / 50 · English

Java Arrays

Store related values in a fixed-size sequence.

Step-by-step explanationsRunnable JavaPractice + answer

Read this topic in தமிழ் ↗.

By the end of this lesson, you can…

  • Explain the key idea behind Arrays.
  • Read and run a complete example, then predict what changes.
  • Recognize the topic-specific mistake discussed below.
01 / The concept

Understand the idea

An array has a fixed length after creation. Elements use zero-based indexes, so valid indexes range from 0 through length – 1. Changing one element is different from resizing the array.

02 / Step by step

Explore the details

Java Arrays

An array stores multiple values of the same type in a single variable — like a row of numbered boxes, each holding one item.

Instead of writing String car1 = "Volvo", String car2 = "BMW"… you store them all in one array and access each by its index.

Declaring and Initialising an Array

Code anatomy / கோடின் விளக்கம்

CodeMeaning
StringThe type of every element in the array
[]Square brackets declare this as an array
carsThe array variable name
{Opening brace — list your values inside
"Volvo"Element at index 0
"BMW"Element at index 1
"Ford"Element at index 2
}Closing brace

Loop Through an Array

Use a for loop with .length to visit every element:

Index starts at 0, not 1. An array of 4 elements has indexes 0, 1, 2, 3. Accessing index 4 throws an ArrayIndexOutOfBoundsException. Use .length - 1 to safely reach the last element.

Java reference fragmentJAVA
int[] nums = {10, 20, 30, 40, 50};
System.out.println(nums.length);      // 5
System.out.println(nums[2]);          // 30
nums[2] = 99;

// for-each traversal
for (int n : nums) System.out.print(n + " ");  // 10 20 99 40 50

// Sort
java.util.Arrays.sort(nums);
System.out.println(java.util.Arrays.toString(nums));  // [10, 20, 40, 50, 99]

// 2D array
int[][] grid = {{1,2,3},{4,5,6},{7,8,9}};
System.out.println(grid[1][2]);  // 6
03 / Read and predict

A complete runnable example

This complete Main.java program focuses on the lesson idea. Reference fragments above may demonstrate individual statements; this example includes the imports, entry point, and supporting types it needs.

Main.java · complete runnable exampleJAVA
public class Main {
    public static void main(String[] args) throws Exception {
        int[] scores = {10, 20, 30};
        scores[1] = 25;
        System.out.println(scores.length);
        for (int score : scores) {
            System.out.println(score);
        }
    }
}
Predict first. Then reveal the expected output / வெளியீடு
3
10
25
30

The output shown was checked by running this complete example. Trace the executed statements in order, then compare with your run.

04 / Learn by changing

Change it. Run it. Understand it.

Load this lesson example into the editor. Change one value or statement, predict the result, then press RUN to execute Java.

Open Java editor ↗
After the editor loads, choose Load this lesson example, then press RUN.

Powered by OneCompiler. Code you run is sent to OneCompiler for execution. For Scanner examples, supply input in the editor I/O panel; for file examples, files exist in its execution environment.

05 / Your turn

Make it your own

Practice challenge

Add a fourth value to the initializer and run the same loop.

Reveal the answer and reasoning / விடை

The length becomes 4 and the for-each loop visits all four values.

One mistake to avoid: The last valid index is length – 1. Accessing scores[scores.length] throws an exception.

Your learning checkpoint

  • Explain the example without reading the output first.
  • Make the change requested in the practice task.
  • Describe why the result changed—or why compilation failed.

Ready to move on? Follow the next lesson in course order, or return to the curriculum to review a prerequisite.

Coding Tamilan · Understand the idea. Try the code. Build the habit.