
Java Arrays
👉What is a Java Array?
An array in Java is a data structure that stores multiple values of the same data type in a single variable. Arrays allow efficient data management by grouping elements under a common name. The first element starts at index 0
. Java arrays inherit from the Object
class and implement Serializable
and Cloneable
interfaces. They can store both primitive data types and objects.
👉Why Use Java Arrays?
Instead of declaring multiple variables like
int x0 = 0;
int x1 = 1;
int x2 = 2;
int x3 = 3;
We can use an array:
int x[] = {0, 1, 2, 3};
This helps in easy looping and efficient memory usage:
for(int count = 0; count < x.length; count++) {
System.out.println(x[count]);
}
👉Types of Arrays in Java:-
- Single-Dimensional Array – A simple array with elements in a single row.
- Multidimensional Array – An array containing multiple arrays (like a matrix).
Declaring, Constructing, and Initializing Java Arrays:
1. Declaring an Array:
Syntax:
<dataType>[] <arrayName>;
<dataType> <arrayName>[];
Example:
int[] intArray;
2. Constructing an Array:
We allocate memory using new
:
intArray = new int[5];
3. Initializing an Array:
intArray[0] = 10;
intArray[1] = 20;
Shorter way:
int[] intArray = {10, 20, 30, 40};
👉Java Array Example:-
class ArrayDemo {
public static void main(String args[]) {
int array[] = new int[5];
for (int count = 0; count < 5; count++) {
array[count] = count + 1;
}
for (int count = 0; count < 5; count++) {
System.out.println(“array[” + count + “] = ” + array[count]);
}
}
}
Output:
array[0] = 1
array[1] = 2
array[2] = 3
array[3] = 4
array[4] = 5
👉Java Array Length Property:-
System.out.println(“Length of Array = ” + array.length);
Output:
Length of Array = 5.
👉Java Array Index Out of Bounds Exception:-
Java checks array bounds and throws an exception if accessed beyond its size:
array[6] = 10; // Throws ArrayIndexOutOfBoundsException.
👉Java Arrays are Passed by Reference:-
Arrays in Java are passed by reference, meaning changes made inside a method affect the original array.
class ArrayDemo {
public static void passByReference(String a[]) {
a[0] = “Updated”;
}
public static void main(String args[]) {
String[] fruits = {“Apple”, “Mango”, “Orange”};
System.out.println(“Before Function Call: ” + fruits[0]);
passByReference(fruits);
System.out.println(“After Function Call: ” + fruits[0]);
}
}
Output:
Before Function Call: Apple.
After Function Call: Updated.
👉Multidimensional Arrays in Java:-
A multidimensional array is an array of arrays. It is commonly used for matrices or tables.
int[][] twoD = new int[3][3];
twoD[0][0] = 1;
twoD[1][1] = 2;
twoD[2][2] = 3;
System.out.println(twoD[0][0]);
Output:
1
👉Conclusion:-
- Java arrays allow you to store multiple elements of the same data type.
- They support both single-dimensional and multidimensional structures.
- Arrays are passed by reference, and Java performs boundary checks to prevent memory corruption.
- Knowing how to declare, initialize, and manipulate arrays effectively is crucial for Java programming.
Related Topics:
- ArrayList in Java.
- Difference Between Arrays and Collections.
- Sorting Arrays in Java.
- Searching Elements in an Array.