Ever tried to line up your school notebooks in order? That's what an array does for data.

💡 In Simple Words: An array is a single box that holds many tiny boxes, each with the same kind of thing, like a row of numbered lockers. You can grab any locker by its number.

What is an Array in Java?

In Java, an array (a fixed‑size list) stores a collection of elements that are all the same type. Think of it as a train with a set number of carriages; each carriage holds one passenger, and you know exactly how many carriages there are.

Declaration and Creation

First you tell Java what kind of thing you want to store, then you ask for a certain number of slots.

int[] marks;               // declaration – says "I will have an array of integers"
marks = new int[5];       // creation – asks Java for 5 slots

You can combine both steps:

int[] marks = new int[5];

Initialization – putting values inside

After the array exists, you can fill each slot, just like putting books on each shelf.

marks[0] = 78;   // first slot (index 0)
marks[1] = 85;
marks[2] = 92;
marks[3] = 67;
marks[4] = 88;

Or you can do it all at once:

int[] marks = {78, 85, 92, 67, 88};

Accessing Elements

Use the index (the locker number) inside square brackets.

int highest = marks[2];   // gets the value 92

Remember, Java starts counting at 0, so the last element of a 5‑slot array is at index 4.

Array Length

Every array knows how many slots it has. The length property gives you that number.

int total = marks.length;   // total is 5

Why Use Arrays?

  • Save time – you don’t need a separate variable for each value.
  • Easy to loop – you can run the same code for every element.
  • Predictable size – the number of slots never changes, which helps the computer manage memory.

Common Operations

Below is a quick cheat‑sheet of things you’ll do with arrays in exams.

OperationCode ExampleWhat it does
Declareint[] a;Tell Java you will store integers.
Createa = new int[3];Allocate three slots.
Initializea = {1,2,3};Fill slots with values.
Accessint x = a[1];Read the second value (2).
Modifya[2] = 10;Change third value to 10.
Lengtha.lengthGet number of slots (3).
Loopfor(int i=0;iPrint every element.

Working with Arrays: A Sample Question

Suppose the exam asks you to store the scores of five subjects and print the average.

public class AvgScore {
    public static void main(String[] args) {
        int[] score = {78, 85, 92, 67, 88};
        int sum = 0;
        for(int i = 0; i 

Key points to notice:

  • We used int[] score = {…} to both create and initialize.
  • The for loop runs from 0 to score.length‑1, ensuring every element is visited.
  • We cast sum to double so the division gives a decimal result.

Tips for ICSE Exams

  • Always write the index range correctly: 0 to array.length‑1.
  • Remember that arrays cannot change size after creation – if you need more slots, you must create a new larger array.
  • When printing, use System.out.println inside the loop for each element, or build a single string first.
  • Watch out for ArrayIndexOutOfBoundsException – that fancy term just means you tried to reach a locker that doesn't exist.

📝 Likely Exam Questions

  1. Write a Java program to store the names of three friends in a String array and display them.
    String[] friends = {"Asha", "Rohit", "Meena"};
    for(int i=0;i
  2. Explain what happens if you try to access arr[5] in an array declared as int[] arr = new int[5];
    You will get an ArrayIndexOutOfBoundsException because valid indices are 0‑4.
  3. Given int[] a = {2,4,6,8};, write the output of the following code:
    int sum = 0;
    for(int i=0;i
    Answer: 20
  4. How do you find the number of elements stored in an array called data?
    Use data.length.
  5. What is the difference between declaration and initialization of an array?
    Declaration tells Java the type and name (e.g., int[] x;). Initialization actually creates the slots and may assign values (e.g., x = new int[3]; or x = {1,2,3};).
#ICSE#Class 10#Computer Applications#Java#Arrays