Imagine you have the marks of five students. You could create five separate variables:
int mark1 = 85;
int mark2 = 72;
int mark3 = 91;
int mark4 = 68;
int mark5 = 79;
This works, but it becomes inconvenient when you have 50, 500, or even 5,000 values.
Instead of creating a separate variable for every value, Java gives us arrays.
An array allows us to store multiple values of the same data type in a single variable.
Creating an Array
Let's create an array that stores five marks:
int[] marks = {85, 72, 91, 68, 79};
Here, marks is an array containing five integer values.
You can think of an array like a row of boxes:
marks
┌────┬────┬────┬────┬────┐
│ 85 │ 72 │ 91 │ 68 │ 79 │
└────┴────┴────┴────┴────┘
Each box contains one value.
Array Index
Every value in an array has a position called an index.
The important thing to remember is that Java arrays start counting from 0, not 1.
So our array looks like this:
Index: 0 1 2 3 4
Value: 85 72 91 68 79
The first value is at index 0, the second is at index 1, and so on.
We can access a value by using its index.
int[] marks = {85, 72, 91, 68, 79};
System.out.println(marks[0]);
Output:
85
To access the third value:
System.out.println(marks[2]);
Output:
91
This zero-based indexing is very important when working with arrays.
Changing an Array Value
Array elements can be changed after the array is created.
For example:
int[] marks = {85, 72, 91, 68, 79};
marks[1] = 80;
The array is now:
85, 80, 91, 68, 79
We changed the value at index 1 from 72 to 80.
Creating an Array with a Fixed Size
We can also create an array by specifying its size.
int[] marks = new int[5];
This creates an integer array that can hold 5 values.
We can then assign values using indexes:
marks[0] = 85;
marks[1] = 72;
marks[2] = 91;
marks[3] = 68;
marks[4] = 79;
The array now contains five values.
One important thing to understand is that the size of a Java array is fixed after it is created. If you create an array with five elements, you cannot make that same array hold six elements later. If you need a dynamically growing collection, Java provides other options such as ArrayList, which we will learn separately.
Finding the Length of an Array
Java provides the length property to find the number of elements in an array.
int[] marks = {85, 72, 91, 68, 79};
System.out.println(marks.length);
Output:
5
This is especially useful when working with loops because you don't always know the size of an array beforehand.
Using a Loop with an Array
Arrays and loops are commonly used together.
For example:
int[] marks = {85, 72, 91, 68, 79};
for (int i = 0; i < marks.length; i++) {
System.out.println(marks[i]);
}
Output:
85
72
91
68
79
Here, i starts at 0 and continues until it reaches the last index.
The expression:
marks[i]
means that Java accesses the element at the current index.
This is one of the most common ways you'll work with arrays.
Arrays Can Store Different Types
An array can store values of different primitive data types, but all elements in a particular array must have the same type.
For example, an integer array:
int[] numbers = {10, 20, 30};
A double array:
double[] prices = {99.99, 49.50, 25.75};
A character array:
char[] grades = {'A', 'B', 'C'};
And a String array:
String[] names = {"John", "Jason", "Alex"};
You cannot normally mix unrelated types in a primitive array like this:
int[] numbers = {10, 20, "Hello"}; // Error
because "Hello" is a String, not an integer.
Accessing an Invalid Index
You also need to be careful when accessing array elements.
Suppose we have:
int[] numbers = {10, 20, 30};
The valid indexes are:
0
1
2
If we try:
System.out.println(numbers[3]);
Java will throw an ArrayIndexOutOfBoundsException because index 3 doesn't exist.
Remember:
If an array has 3 elements, its indexes are 0, 1, and 2.
A Simple Example
Let's create a small program that stores and displays the marks of five students:
class Main {
public static void main(String[] args) {
int[] marks = {85, 72, 91, 68, 79};
for (int i = 0; i < marks.length; i++) {
System.out.println("Mark: " + marks[i]);
}
}
}
Output:
Mark: 85
Mark: 72
Mark: 91
Mark: 68
Mark: 79
Instead of creating five separate variables, we stored all five marks inside one array and used a loop to process them.
That's the main advantage of arrays: they let us work with a collection of related values in an organized way.