An array is a data structure that stores multiple values of the same declared type under a single variable name. Each element is accessed using its index.
- Arrays can store primitive values or references to objects.
- The length of an array is fixed once it is created.
- Array indexing starts from 0.
public class Geeks{
public static void main(String[] args){
// Primitive array
int[] arr = {10, 20, 30, 40};
System.out.print("Primitive Array: ");
for (int num : arr) {
System.out.print(num + " ");
}
System.out.println();
// Reference-type array
String[] names = {"Lakshit", "Rahul", "Pankaj"};
System.out.print("Reference Array: ");
for (String name : names) {
System.out.print(name + " ");
}
}
}
Output
Primitive Array: 10 20 30 40 Reference Array: Lakshit Rahul Pankaj
Explanation: The arr array stores integer values, while the names array stores references to String objects. Both arrays are traversed using enhanced for loops.

Declaring an Array
An array is declared by specifying its element type and name using square brackets.
Syntax:
dataType[] arrayName;
// Alternative syntax
dataType arrayName[];
The first syntax is preferred because it clearly associates the brackets with the data type.
Initializing an Array
Arrays can be created and initialized using the new keyword or an array initializer.
1. Using the new Keyword
int[] arr = new int[5];
This creates an integer array that can hold five elements. Java automatically initializes array elements with default values:
- 0 for numeric types.
- '\u0000' for char.
- false for boolean.
- null for reference types.
2. Using an Array Initializer
We can initialize an array directly by specifying its elements inside curly braces.
int[] arr = {10, 20, 30, 40, 50};

Java determines the array length from the number of values provided in the initializer.
Operations on Array Elements
Java supports several basic operations on arrays.
- Accessing: Retrieve an element using its index.
- Updating: Modify an element using the assignment operator.
- Traversing: Visit each element using a loop.
- Finding length: Use the length property to get the number of elements.
class Geeks{
public static void main(String[] args){
int[] arr = {2, 4, 8, 12, 16};
// Access an element
System.out.println("Fourth element: " + arr[3]);
// Update an element
arr[0] = 90;
// Traverse the array
System.out.print("Array elements: ");
for (int i = 0; i < arr.length; i++){
System.out.print(arr[i] + " ");
}
// Find array length
System.out.println("\nArray length: " + arr.length);
}
}
Output
Fourth element: 12 Array elements: 90 4 8 12 16 Array length: 5
Explanation: The program accesses the fourth element, updates the first element, traverses the array using a for loop and displays its length.
Note: Valid array indices range from 0 to arr.length - 1. Accessing an index outside this range throws an ArrayIndexOutOfBoundsException.
Arrays of Objects
An array of objects stores references to objects rather than the objects themselves. Each element is initially set to null when the array is created using the new keyword.
class Student{
int rollNo;
String name;
Student(int rollNo, String name){
this.rollNo = rollNo;
this.name = name;
}
}
public class Geeks{
public static void main(String[] args){
// Create an array of three null references
Student[] arr = new Student[3];
// Create objects and store their references
arr[0] = new Student(1, "Aman");
arr[1] = new Student(2, "Rahul");
arr[2] = new Student(3, "Mohit");
// Traverse the array
for (Student s : arr){
System.out.println(s.rollNo + " " + s.name);
}
}
}
Output
1 Aman 2 Rahul 3 Mohit
Explanation: The new Student[3] statement creates an array containing three null references. Individual Student objects are created separately using their constructor.
Passing and Returning Arrays from Methods
Java allows arrays to be passed as arguments and returned from methods.
1. Passing an Array: When an array is passed to a method, a copy of its reference is passed. Changes to its elements inside the method affect the original array.
static void update(int[] arr) {
arr[0] = 100;
}
2. Returning an Array: A method can return an array reference by specifying an array type as its return type.
static int[] getArray() {
return new int[] {10, 20, 30};
}
Advantages of Java Arrays
- Provides constant-time O(1) access using an index.
- Stores related elements under a single variable name.
- Supports easy traversal and manipulation using loops.
Limitations of Java Arrays
- Array length cannot be changed after creation.
- Elements must be compatible with the declared element type.
- Inserting or deleting elements at intermediate positions may require shifting existing elements.
Related Articles:

