Java Arrays Tutorial: Declaration, Creation, and Initialization – Example Code
What is a Java Array?
In Java, an array is a built‑in data structure that holds a fixed‑size sequence of elements all of the same type. Elements are indexed starting at 0. Arrays are objects that inherit from Object and implement the Serializable and Cloneable interfaces, so they can store primitive values or references to objects.
In practice, an array lets you replace code such as:
x0=0; x1=1; x2=2; x3=3; x4=4; x5=5;
with a concise, indexed form:
x[0]=0; x[1]=1; x[2]=2; x[3]=3; x[4]=4; x[5]=5;
This tutorial covers:
- What an array is
- Array variables and the 3‑step creation process
- Your first array program
- Arrays passed by reference
- Multidimensional arrays
Having an array reference lets you loop efficiently:
for(count=0; count<5; count++) {
System.out.println(x[count]);
}
Array Variables
Using an array in Java follows a simple three‑step process:
- Declare the array type.
- Instantiate the array with a size.
- Optional: Initialize its elements.
1) Declaring an Array
Syntax:
<elementType>[] <arrayName>;
or
<elementType> <arrayName>[];
Example:
int intArray[]; // declares an array that will hold int values int []intArray; // same declaration
2) Constructing an Array
Allocate memory for the array:
arrayName = new dataType[size];
Example:
intArray = new int[10]; // creates space for 10 integers
Combined declaration and construction:
int intArray[] = new int[10];
3) Initializing an Array
Assign values individually:
intArray[0] = 1; intArray[1] = 2;
Or provide an initializer list at declaration:
int intArray[] = {1, 2, 3, 4};
This creates an array of length 4 with the given values.
First Array Program
Step 1: Copy the code into an editor.
class ArrayDemo{
public static void main(String args[]){
int array[] = new int[7];
for (int count=0; count<7; count++){
array[count] = count + 1;
}
for (int count=0; count<7; count++){
System.out.println("array[" + count + "] = " + array[count]);
}
// System.out.println("Length of Array = " + array.length);
// array[8] = 10;
}
}
Step 2: Compile and run. Output:
array[0] = 1 array[1] = 2 array[2] = 3 array[3] = 4 array[4] = 5 array[5] = 6 array[6] = 7
Step 3: Using array.length returns the array’s size.
Uncomment line 10 and run again:
Length of Array = 7
Step 4: Java enforces bounds checking. Uncomment line 11 to see the exception:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 8
at ArrayDemo.main(ArrayDemo.java:11)
Command exited with non‑zero status 1
In contrast, C would silently read or write out of bounds.
Arrays Passed by Reference
Array parameters are passed by reference, meaning any modification inside a method affects the original array.
Example:
class ArrayDemo {
public static void passByReference(String a[]){
a[0] = "Changed";
}
public static void main(String args[]){
String []b = {"Apple","Mango","Orange"};
System.out.println("Before Function Call " + b[0]);
ArrayDemo.passByReference(b);
System.out.println("After Function Call " + b[0]);
}
}
Output:
Before Function Call Apple After Function Call Changed
Multidimensional Arrays
A multidimensional array is simply an array of arrays. Declaring it requires an additional pair of brackets for each dimension:
int twoD[][] = new int[4][5];
When allocating, you need only specify the first (leftmost) dimension. Subsequent dimensions can be allocated later, giving you fine control over memory layout.
Example:
public class Guru99 {
public static void main(String[] args) {
// Create a 4x4 two‑dimensional array.
int[][] twoD = new int[4][4];
// Assign a few elements.
twoD[0][0] = 1;
twoD[1][1] = 2;
twoD[3][2] = 3;
System.out.print(twoD[0][0] + " ");
}
}
Output:
1
Java
- Mastering Java Arrays: Declaration, Initialization, and Manipulation
- Mastering Java Multidimensional Arrays: 2D & 3D Examples
- Java Array Copying Techniques: A Comprehensive Guide
- C++ Arrays Demystified: Declaration, Initialization, and Pointer Techniques
- Master C# Arrays: Declaring, Initializing, and Working with Integer Collections
- Creating an Array of Objects in Java: A Step‑by‑Step Guide
- Java Swing Tutorial: Building Robust GUI Applications
- Master Java Arrays: A Comprehensive Guide to Efficient Data Handling
- C Arrays Explained: Efficient Data Storage & Access
- Mastering Arrays in C#: Efficient Data Storage & Access