Unit 4 Cycle 1 Day 1: Array Declaration and Initialization

Unit 4 Foundation (Cycle 1) Day 1 of 28 Foundation

Array Declaration and Initialization

Section 4.1 — Array Creation and Access

Key Concept

Arrays in Java are fixed-size, ordered collections of elements of the same type. Declaration uses square brackets: int[] arr = new int[5] creates an array of 5 integers, all initialized to 0. Array elements are accessed by index starting at 0, so valid indices for an array of length n range from 0 to n - 1. Once created, an array's length cannot change. The length property (not a method — no parentheses) returns the number of elements.

Consider the following code segment.

int[] nums = new int[5]; nums[0] = 10; nums[1] = 20; System.out.println(nums[2]);

What is printed?

Answer: (A) 0

When an int array is created with new int[5], all elements are initialized to 0 by default. Only indices 0 and 1 are explicitly set. Index 2 retains its default value of 0.

Why Not the Others?

(B) 20 is at index 1, not index 2.

(C) null is the default for object arrays, not primitive arrays. int defaults to 0.

(D) Index 2 is within bounds (0-4). No ArrayIndexOutOfBoundsException.

Common Mistake

Primitive arrays default to 0 (int), 0.0 (double), or false (boolean). Object arrays default to null. Unassigned elements are NOT empty or undefined.

AP Exam Tip

Know the default values: int=0, double=0.0, boolean=false, String/Object=null. This is tested frequently on the AP exam.

Review this topic: Section 4.1 — Array Creation and Access • Unit 4 Study Guide
Back to blog

Leave a comment

Please note, comments need to be approved before they are published.