Why bother with arrays?
Imagine you have a row of lockers, each holding a single textbook. Instead of remembering 30 separate names, you just remember the locker number. In Java, an array is that row of lockers for data.
💡 In Simple Words: An array stores many values of the same type in one place. You give each value an index (starting at 0) and pull it out whenever you need it, just like opening a specific locker.
What is an Array in Java?
An array is a fixed‑size container that holds elements of the same data type—numbers, characters, or objects. The size is set when you create the array and can’t change later. Think of it as a train with a set number of cars; you can load passengers (data) into each car, but you can’t add more cars after the train leaves the station.
How to Declare and Initialize an Array
The first step is telling Java you want an array. This is called declaration. The second step is giving the array a size or putting values straight away—this is initialization.
- Declaration only:
int[] marks; - Declaration + size:
marks = new int[5]; - Declaration + values:
int[] marks = {85, 90, 78, 92, 88};
Notice the square brackets []—they tell Java “this is an array”. The type before the brackets (like int) says what kind of data each slot will hold.
Accessing Array Elements
Each slot has an index—a number starting at 0. To read or change a value, you write the array name followed by the index in square brackets.
- Read:
int first = marks[0]; // gets 85 - Write:
marks[2] = 80; // changes 78 to 80
If you try an index that’s outside the range (like marks[5] in a 5‑element array), Java throws an ArrayIndexOutOfBoundsException—a fancy way of saying “you’re opening a locker that doesn’t exist”.
Common Array Operations
Here are a few tasks you’ll need for most ICSE questions.
- Length:
marks.lengthgives the total number of slots. - Looping: The classic
forloop lets you visit every locker.for(int i = 0; i - Finding the maximum: Compare each element while looping.
int max = marks[0]; for(int i = 1; i max) max = marks[i]; }
Sample Java Array Program
Let’s put everything together. The program reads five scores, stores them in an array, prints the scores, and shows the highest score.
import java.util.Scanner;
public class ScoreArray {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int[] scores = new int[5];
System.out.println("Enter 5 marks:");
for(int i = 0; i max) max = scores[i];
}
System.out.println("\nHighest mark = " + max);
}
}
Key points to notice:
- Array size is fixed at 5.
- We used two loops: one to fill the array, another to display it.
- The
for‑eachloop (written asfor(int mark : scores)) is a shortcut when you only need the value, not the index.
Quick Summary
| Topic | Syntax | What it does |
|---|---|---|
| Declare array | type[] name; | Creates a reference to an array, no memory yet. |
| Allocate size | name = new type[size]; | Reserves memory for the given number of elements. |
| Initialize with values | type[] name = {v1, v2, …}; | Both declares and fills the array. |
| Access element | name[index] | Read or write the value at that position. |
| Length of array | name.length | Returns total slots, useful for loops. |
📝 Likely Exam Questions
- Write a Java program to store the names of 4 friends in a String array and print them using a for‑each loop.
Model answer:String[] friends = {"Asha", "Rohit", "Neha", "Kiran"}; for(String f : friends) { System.out.println(f); } - How do you find the sum of all elements in an integer array called
arr?
Model answer:int sum = 0; for(int i = 0; i - Explain what happens if you try to access
arr[10]whenarrwas created asnew int[5].
Model answer: Java throws anArrayIndexOutOfBoundsExceptionbecause the valid indices are 0 to 4. - Declare a double array named
pricesthat can hold 3 values and initialize it with 99.9, 149.5, and 200.0 in a single line.
Model answer:double[] prices = {99.9, 149.5, 200.0}; - What is the output of the following code?
int[] a = {2,4,6}; System.out.println(a.length);
Model answer: The output is3becauselengthreturns the number of elements.