An array in Java is used to store multiple values of the same data type in a single variable. Each element is accessed using an index, and array indexes start at 0.
Suppose you want to store five student marks. Instead of creating five separate variables, you can use an array.
int[] marks = {75, 80, 85, 90, 95};
System.out.println(marks[0]);
System.out.println(marks[1]);
System.out.println(marks[2]);
Output:
75
80
85
Without an array:
int mark1 = 75;
int mark2 = 80;
int mark3 = 85;
Using an array:
int[] marks = {75, 80, 85};
Arrays have a fixed length after creation. If you need a
collection that can grow or shrink, Java also provides
classes such as ArrayList.
You can declare an array in either of these ways:
int[] numbers;
String[] names;
double[] prices;
The declaration specifies the type of elements the array can store.
| Declaration | Element Type |
|---|---|
int[] numbers; |
Integers |
double[] prices; |
Decimal numbers |
char[] letters; |
Characters |
String[] names; |
Strings |
boolean[] answers; |
Boolean values |
You can create an array by specifying its length using
the new keyword.
int[] numbers = new int[5];
This creates an integer array containing five elements. The default value of each element is 0.
System.out.println(numbers[0]);
System.out.println(numbers[1]);
System.out.println(numbers[4]);
Output:
0
0
0
Default values include 0 for integer arrays, 0.0 for floating-point arrays, false for boolean arrays, and null for reference-type arrays such as String arrays.
You can initialize an array with values when declaring it.
int[] numbers = {10, 20, 30, 40, 50};
String[] names = {"Amit", "Rahul", "Priya"};
double[] prices = {99.50, 150.75, 250.00};
Example:
String[] names = {"Amit", "Rahul", "Priya"};
System.out.println(names[0]);
System.out.println(names[1]);
System.out.println(names[2]);
Output:
Amit
Rahul
Priya
Each array element has an index. The first index is 0, the second is 1, and so on.
int[] numbers = {10, 20, 30, 40, 50};
System.out.println(numbers[0]);
System.out.println(numbers[2]);
System.out.println(numbers[4]);
Output:
10
30
50
| Index | Value |
|---|---|
| 0 | 10 |
| 1 | 20 |
| 2 | 30 |
| 3 | 40 |
| 4 | 50 |
You can change an array element by assigning a new value to its index.
int[] numbers = {10, 20, 30, 40};
numbers[1] = 100;
System.out.println(numbers[1]);
Output:
100
The original value 20 at index 1 is replaced with 100.
Complete example:
int[] marks = {60, 70, 80};
marks[0] = 95;
marks[2] = 90;
for (int i = 0; i < marks.length; i++) {
System.out.println(marks[i]);
}
Output:
95
70
90
The length property returns the number of
elements in an array.
int[] numbers = {10, 20, 30, 40, 50};
System.out.println(numbers.length);
Output:
5
Use the length property to loop through every element.
int[] numbers = {10, 20, 30, 40, 50};
for (int i = 0; i < numbers.length; i++) {
System.out.println(numbers[i]);
}
array.length, not
array.length().
A for loop is commonly used to access every array element.
public class Main {
public static void main(String[] args) {
int[] numbers = {10, 20, 30, 40, 50};
for (int i = 0; i < numbers.length; i++) {
System.out.println(numbers[i]);
}
}
}
Output:
10
20
30
40
50
You can also use an enhanced for loop:
int[] numbers = {10, 20, 30, 40, 50};
for (int number : numbers) {
System.out.println(number);
}
The enhanced for loop is useful when you want to read each element without managing an index.
Calculate the sum and average of five marks.
public class Main {
public static void main(String[] args) {
int[] marks = {70, 80, 90, 60, 100};
int sum = 0;
for (int mark : marks) {
sum += mark;
}
double average = (double) sum / marks.length;
System.out.println("Total = " + sum);
System.out.println("Average = " + average);
}
}
Output:
Total = 400
Average = 80.0
Casting the sum to double ensures the average
can include a decimal part when necessary.
Find the largest and smallest values in an integer array.
public class Main {
public static void main(String[] args) {
int[] numbers = {25, 10, 85, 40, 5};
int largest = numbers[0];
int smallest = numbers[0];
for (int i = 1; i < numbers.length; i++) {
if (numbers[i] > largest) {
largest = numbers[i];
}
if (numbers[i] < smallest) {
smallest = numbers[i];
}
}
System.out.println("Largest = " + largest);
System.out.println("Smallest = " + smallest);
}
}
Output:
Largest = 85
Smallest = 5
This example assumes the array contains at least one element. An empty array must be handled separately before accessing index 0.
| Error | Explanation or Correction |
|---|---|
| Accessing index 5 in a five-element array | Valid indexes are 0 through 4. |
| Using different element types in a primitive int array | Store values compatible with the declared type. |
Using numbers.length() |
Use numbers.length for an array. |
Using i <= numbers.length |
Use i < numbers.length. |
| Accessing an array before initializing its reference | Initialize the array before using it. |
Incorrect example:
int[] numbers = {10, 20, 30};
System.out.println(numbers[3]);
This throws an ArrayIndexOutOfBoundsException
because the valid indexes are 0, 1, and 2.
Correct example:
System.out.println(numbers[2]);
Output: 30
Write Java programs to complete these assignments.
length property before moving to multidimensional arrays.
1. What is the index of the first element in a Java array?
2. Which property returns the number of elements in an array?
3. What happens when you access an invalid array index?