⏱️ 12 min read • Beginner Level • Lesson 37
An array in Java is an object that stores multiple values of the same data type in a single variable. Arrays are useful when you need to store and process a group of related values.
Before learning arrays, you should understand Java Variables, Java Data Types, and Java Loops. After this lesson, continue with Java Strings.
An array is a collection of similar data items stored under one variable name. Each value in an array is called an element, and each element is accessed using an index.
int[] marks = {80, 75, 90};
In this example, marks is an integer array that stores three integer values.
Arrays are commonly used when we need to store multiple similar values.
For example, instead of creating separate variables:
int mark1 = 80;
int mark2 = 75;
int mark3 = 90;
We can store all values in a single array:
int[] marks = {80, 75, 90};
0.length property.Array declaration means creating a reference variable that can point to an array object. You can place square brackets on the left or right side of the variable name, but the left-side style is recommended.
int[] numbers; // recommended
int numbers2[]; // works, but not recommended
int[] numbers; because it clearly shows that the type is an array of integers.
Array construction means creating the actual array object in memory using the new keyword.
Java needs to know the size of the array at the time of construction.
int[] numbers; // declaration
numbers = new int[4]; // construction, size is mandatory
This creates an integer array object that can store four integer values.
Array initialization means assigning values to array elements. Array elements are accessed using index numbers.
int[] numbers = new int[5];
numbers[0] = 10;
numbers[1] = 20;
numbers[2] = 30;
numbers[3] = 40;
numbers[4] = 50;
0 to 4.
When an array is constructed, each element gets the default value of its data type.
| Array Type | Default Value |
|---|---|
int[] |
0 |
double[] |
0.0 |
boolean[] |
false |
char[] |
'\u0000' |
| Object reference array | null |
public class ArrayDefaultValues {
public static void main(String[] args) {
int[] numbers = new int[4];
for (int i = 0; i < numbers.length; i++) {
System.out.println(numbers[i]);
}
}
}
Output:
0 0 0 0
new int[4] creates an integer array with four elements.0.length property.
Every array has a length property that stores the size of the array.
Remember, length is a property, not a method, so do not write parentheses.
public class ArrayLengthExample {
public static void main(String[] args) {
int[] numbers = {10, 20, 30};
System.out.println("Array size: " + numbers.length);
}
}
Output:
Array size: 3
If you try to access an index outside the valid range,
Java throws an ArrayIndexOutOfBoundsException.
public class ArrayIndexOutOfBoundsExceptionExample {
public static void main(String[] args) {
int[] numbers = {10, 20, 30};
System.out.println(numbers[3]);
}
}
Valid indexes are 0, 1 and 2. Index 3 does not exist.
A single dimensional array stores values in a linear form. It is the most common type of array.
public class SingleDimensionalArray {
public static void main(String[] args) {
int[] numbers = new int[5];
numbers[0] = 10;
numbers[1] = 20;
numbers[2] = 30;
numbers[3] = 40;
numbers[4] = 50;
for (int i = 0; i < numbers.length; i++) {
System.out.println(numbers[i]);
}
}
}
Output:
10 20 30 40 50
new int[5] creates an array with five integer elements.0 to 4.for loop starts from index 0.numbers.length is used to avoid hardcoding the array size.numbers[i].Java supports different ways to declare, instantiate and initialize arrays.
int[] arr1;
arr1 = new int[5];
int[] arr2 = new int[5];
int[] arr3 = {10, 20, 30};
int[] arr4 = new int[] {10, 20, 30};
int[] arr3 = {10, 20, 30}; is a short and commonly used way to declare and initialize an array.
A for-each loop is useful when you want to read all elements of an array without using indexes.
public class ForEachArrayExample {
public static void main(String[] args) {
int[] numbers = {10, 20, 30};
for (int number : numbers) {
System.out.println(number);
}
}
}
Output:
10 20 30
Arrays can be passed to methods. This is useful when you want a method to process all elements of an array. Your old example for finding sum of array elements fits perfectly here.
public class ArraySumUsingMethod {
static void getSum(int[] numbers) {
int sum = 0;
for (int number : numbers) {
sum = sum + number;
}
System.out.println("SUM = " + sum);
}
public static void main(String[] args) {
int[] values = {1, 2, 3};
getSum(values);
}
}
Output:
SUM = 6
values is an integer array with values 1, 2, 3.getSum(values) passes the array to the method.int[] numbers.6.A multidimensional array is an array of arrays. The most common multidimensional array is a two-dimensional array.
int[][] matrix;
int matrix2[][];
int[] matrix3[];
Recommended style is int[][] matrix;.
public class TwoDimensionalArray {
public static void main(String[] args) {
int[][] numbers = {
{1, 2, 3},
{4, 5, 6},
{5, 7, 8}
};
for (int i = 0; i < numbers.length; i++) {
for (int j = 0; j < numbers[i].length; j++) {
System.out.print(numbers[i][j] + " ");
}
System.out.println();
}
}
}
Output:
1 2 3 4 5 6 5 7 8
numbers is a two-dimensional array.numbers[i][j] accesses each element.A jagged array is a two-dimensional array where each row can have a different length. In Java, this is possible because multidimensional arrays are arrays of arrays.
public class JaggedArrayExample {
public static void main(String[] args) {
int[][] numbers = new int[3][];
numbers[0] = new int[] {1, 2, 3};
numbers[1] = new int[] {4, 6};
numbers[2] = new int[] {7, 8, 9, 10};
for (int i = 0; i < numbers.length; i++) {
for (int j = 0; j < numbers[i].length; j++) {
System.out.print(numbers[i][j] + " ");
}
System.out.println();
}
}
}
Output:
1 2 3 4 6 7 8 9 10
new int[3][] creates three rows, but column sizes are not fixed yet.numbers[0] has 3 elements.numbers[1] has 2 elements.numbers[2] has 4 elements.
Java arrays can also store object references. For example, you can create an array of Student objects.
class Student {
String name;
Student(String name) {
this.name = name;
}
}
public class ArrayOfObjectsExample {
public static void main(String[] args) {
Student[] students = new Student[3];
students[0] = new Student("Ayan");
students[1] = new Student("Rahul");
students[2] = new Student("Sara");
for (Student student : students) {
System.out.println(student.name);
}
}
}
Output:
Ayan Rahul Sara
0.length() instead of length.array.length.| Concept | Description |
|---|---|
| Array | Collection of same type values |
| Index | Starts from 0 |
| Size | Fixed after creation |
| length | Stores array size |
| Single Dimensional | One row of elements |
| Multidimensional | Array of arrays |
| Jagged Array | Rows with different lengths |
0.length property stores the size of an array.🧠 Test your understanding with a quick quiz
Topic: Arrays | Language: Java
public class Test {
public static void main(String[] args) {
int[] numbers = {10, 20, 30};
System.out.println(numbers[1]);
}
}public class Test {
public static void main(String[] args) {
int[] numbers = {10, 20, 30};
System.out.println(numbers.length);
}
}public class Test {
public static void main(String[] args) {
int[] numbers = new int[2];
System.out.println(numbers[0]);
}
}🎉 Great job! Continue learning Java step by step.