Java Arrays
What is an Array?
An array is a fixed-size container that holds multiple values of the SAME type, stored together under a single variable name. Once you create an array with a given size, that size never changes — you can't grow or shrink it, only replace the values it holds.
Declaring and Creating Arrays
// Create an array of 5 ints, all initialized to 0
int[] numbers = new int[5];
// Create and fill an array in one step
int[] scores = {85, 92, 78, 60, 99};
// Equivalent, more explicit form
int[] scores2 = new int[]{85, 92, 78, 60, 99};int[] arr declares a variable that can hold a reference to an int array.
new int[5] allocates space for exactly 5 ints.
{85, 92, 78, 60, 99} is array literal syntax — Java infers the size from how many values you list.
Zero-Based Indexing
Array elements are numbered starting at 0, not 1. For an array of 5 elements, the valid indexes are 0, 1, 2, 3, and 4 — there is no index 5.
int[] scores = {85, 92, 78, 60, 99};
System.out.println(scores[0]); // first element
System.out.println(scores[4]); // last element
scores[2] = 100; // update the third element
System.out.println(scores[2]);85 99 100
The .length Property
Every array has a .length that tells you how many elements it holds.
int[] scores = {85, 92, 78, 60, 99};
System.out.println("Array size: " + scores.length);Array size: 5
Type | How to get the size |
Array (int[], String[], ...) | arr.length — a field, no parentheses |
String | str.length() — a method, needs parentheses |
List, ArrayList, etc. | list.size() — a method, needs parentheses |
Default Values
When you create an array with new and don't provide initial values, Java fills every slot with a default value based on the element type — the same defaults used for uninitialized instance fields.
Element type | Default value |
int, short, byte, long | 0 |
double, float | 0.0 |
boolean | false |
char | '\u0000' (null character) |
Object references (String, etc.) | null |
int[] nums = new int[3]; boolean[] flags = new boolean[3]; String[] names = new String[3]; System.out.println(nums[0]); // 0 System.out.println(flags[0]); // false System.out.println(names[0]); // null
0 false null
Out-of-Bounds Access: A Real Safety Net
What happens if you try to access an index that doesn't exist? In languages like C, reading past the end of an array is undefined behavior — it might silently read garbage memory, corrupt other data, or crash unpredictably, and the bug can be very hard to track down. Java takes a completely different, much safer approach: every array access is bounds-checked at runtime.
int[] scores = {85, 92, 78, 60, 99};
System.out.println(scores[5]); // no such index — only 0 to 4 existException in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 5 out of bounds for length 5
Practice Exercises
Create an int array of 6 elements, fill it with the first six even numbers, then print it using a loop.
Write a program that intentionally accesses an out-of-bounds index and observe the exception, then fix the loop condition.
Create a String array of size 4 without initializing it, then print each element to confirm the default value is null.