JavaArrays

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

Java
// 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.

Java
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.

Java
int[] scores = {85, 92, 78, 60, 99};
System.out.println("Array size: " + scores.length);
Array size: 5
Note
length on an array is a FIELD, not a method — you write scores.length, with no parentheses. This is genuinely easy to mix up with String's .length() method, which IS a method and DOES need parentheses: "hello".length(). Writing scores.length() is a compile error, and writing "hello".length is also a compile error. Arrays and Strings simply chose different conventions here.

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

Java
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.

Java
int[] scores = {85, 92, 78, 60, 99};
System.out.println(scores[5]); // no such index — only 0 to 4 exist
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 5 out of bounds for length 5
Warning
Accessing an index that is negative or greater than or equal to arr.length throws an ArrayIndexOutOfBoundsException immediately. This is Java stopping you from reading (or writing) memory that doesn't belong to the array — a genuine safety advantage over languages that let out-of-bounds access happen silently.
Tip
The valid index range for an array of length n is always 0 through n - 1. A common off-by-one bug is writing i <= arr.length in a loop condition instead of i < arr.length — always double check that boundary.
Practice Exercises
  1. Create an int array of 6 elements, fill it with the first six even numbers, then print it using a loop.

  2. Write a program that intentionally accesses an out-of-bounds index and observe the exception, then fix the loop condition.

  3. Create a String array of size 4 without initializing it, then print each element to confirm the default value is null.