← Back to Core Java Course | Chapter 5: Methods & Arrays | Lesson 8 of 10

Java Arrays Introduction

An array is a row of boxes that hold the same kind of value, each with a number starting at 0. It is like a row of lockers you open by number.
Syntax
java
dataType[] arrayName = new dataType[size];
dataType[] arrayName = {value1, value2, value3};
arrayName[index] = value;

Declaring and Creating Arrays

An array is a fixed-size, ordered collection of elements that all share the same declared type, and its size is locked in at the moment it's created — unlike an ArrayList, you can't grow or shrink it afterward.

This fixed size is what makes array access extremely fast: the JVM can compute any element's memory offset directly from its index.

Example: Declaring and Creating Arrays

java
// Define the class Main
public class Main {
	// Program entry point: the JVM starts running here
	public static void main(String[] args) {
		// Declare numbers and set it to new int[5]
		int[] numbers = new int[5];
		// Print a line to the console
		System.out.println(numbers.length);
	}
}

Initializing Arrays

Array literals let you declare, allocate, and populate an array in one line, e.g. int[] nums = {1, 2, 3};, which is far more concise than creating the array and assigning each index separately.

This shorthand only works at the point of declaration, not in a later assignment.

Example: Initializing Arrays

java
// Define the class Main
public class Main {
	// Program entry point: the JVM starts running here
	public static void main(String[] args) {
		// Declare nums and set it to {1, 2, 3}
		int[] nums = {1, 2, 3};
		// Print a line to the console
		System.out.println(nums[1]);
	}
}

Accessing Array Elements

Array indices start at 0, so an array of length 5 has valid indices 0 through 4; asking for index 5 (or any negative index) throws an ArrayIndexOutOfBoundsException at runtime rather than failing to compile.

This off-by-one mistake is one of the most common bugs beginners hit with arrays.

Example: Accessing Array Elements

java
public class Main {
	public static void main(String[] args) {
		int[] nums = {10, 20, 30, 40, 50};
		System.out.println(nums[0]);
		System.out.println(nums[4]);
		// nums[5] would throw ArrayIndexOutOfBoundsException
	}
}

Iterating over Arrays

A standard for loop gives you the index if you need it (say, to compare neighboring elements), while an enhanced for-each loop is simpler and safer when you just need each value in turn without tracking position.

For-each also avoids the possibility of an off-by-one indexing bug entirely.

Example: Iterating over Arrays

java
// Define the class Main
public class Main {
	// Program entry point: the JVM starts running here
	public static void main(String[] args) {
		// Declare nums and set it to {1, 2, 3}
		int[] nums = {1, 2, 3};
		// Loop: repeat while the condition holds
		for (int i = 0; i < nums.length; i++) {
			// Print a line to the console
			System.out.println(nums[i]);
		}
		for (int n : nums) {
			// Print a line to the console
			System.out.println(n);
		}
	}
}

Array Properties

The length field (not a method — no parentheses) tells you how many elements an array holds, which you'll use constantly in loop bounds.

Java also provides Arrays.copyOf() and System.arraycopy() for duplicating or resizing array contents into a new array object.

Example: Array Properties

java
// Import java.util.Arrays so it can be used by its short name
import java.util.Arrays;
// Define the class Main
public class Main {
	// Program entry point: the JVM starts running here
	public static void main(String[] args) {
		// Declare nums and set it to {1, 2, 3}
		int[] nums = {1, 2, 3};
		// Print a line to the console
		System.out.println(nums.length);
		// Declare copy and set it to Arrays.copyOf(nums, 3)
		int[] copy = Arrays.copyOf(nums, 3);
		// Print a line to the console
		System.out.println(copy[0]);
	}
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Accessing arr[arr.length], which throws ArrayIndexOutOfBoundsException.
  2. Trying to change the size of an array after creation, since it is fixed.
  3. Forgetting that an array of objects starts with null, so calling a method on an element throws NullPointerException.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.