skip to content

After `new int[3]` and `new String[3]`, what are the element values, and why don't you have to fill the array first?

level: middleimportance: should knowfreq 55%

answer

  1. new T[n] → all default of T
  2. int[]→0, boolean[]→false, char[]→'', refs→null
  3. String[] = nulls, NOT empty strings
  4. no definite-assignment for elements
  5. new int[2][] → inner arrays are null

basics

~20 s

Creating an array fills every slot with the type's default: new int[3] gives {0,0,0} and new String[3] gives {null,null,null}. The array object's memory is zeroed when allocated, so the elements are already safe to read.

solid answer

~40 s

When you allocate an array with `new`, the JVM zero-initializes its backing memory, so every element starts at the default value for the array's element type. `new int[3]` yields three 0s, `new boolean[3]` three falses, `new char[3]` three ''s, and any reference array like `new String[3]` or `new Object[3]` yields all nulls. This is the same defaulting rule that applies to fields, and it's why array elements never require definite assignment the way locals do — the slots are already valid to read. A common bug: `new String[3]` does not contain empty strings, it contains nulls, so iterating and calling a method on each element throws NullPointerException until you populate it. Multidimensional arrays follow the same logic per dimension created.

go deeper

for a junior

Know that new int[n] gives zeros and new String[n] gives nulls, and you can read elements right away.

for a middle

Explain that allocation zeroes element memory using the element-type default, the empty-string-vs-null trap, and that definite assignment doesn't apply to elements.

for a senior

Cover multidimensional nuances (new int[2][] inner nulls), tie it to the unified zeroing rule for object/array memory, and the NPE risk patterns.

for a principal

Discuss memory-model guarantees (safe publication of zeroed array state) and API choices like returning empty arrays vs. arrays-with-nulls to avoid caller NPEs.

## What an array is An **array** in Java is an object that holds a fixed-length, indexed sequence of values, all of the same declared **element type**. You create one with the `new` keyword and a length, e.g. `int[] a = new int[3];`. The variable `a` is a reference to the array object on the heap. ## Allocation zeroes the elements Array creation is a form of object creation. Just as the JVM zeroes a regular object's fields, it zeroes an array's element storage. So immediately after `new`, every slot already holds the **default value of the element type**: - `new int[3]` → `{0, 0, 0}` (and similarly long/short/byte → 0) - `new double[3]` → `{0.0, 0.0, 0.0}` - `new boolean[3]` → `{false, false, false}` - `new char[3]` → three `''` (null characters, code point 0) - `new String[3]`, `new Object[3]`, `new int[3][]` (the outer array of any reference/array type) → all `null` The element default is decided purely by the element type, using the same primitive-vs-reference rule used for fields. ## Why no 'must initialize' requirement **Definite-assignment** analysis applies only to *local variables*, not to array elements or fields. Because the elements are guaranteed-valid defaults from the moment the array exists, reading `a[0]` before you've stored anything is legal and returns the default. The compiler does not (and need not) track which indices you've written. ## The classic null-array trap `String[] names = new String[3];` does **not** create three empty strings; it creates three `null` references. This is a frequent source of `NullPointerException`: ```java String[] names = new String[3]; int len = names[0].length(); // NPE: names[0] is null ``` You must populate the slots first (`names[0] = "a";`) or guard for null. Contrast with `new int[3]`, where `a[0]` is the perfectly usable value `0`. ## Array initializers vs. allocation If you use an initializer, `int[] a = {1, 2, 3};`, you've explicitly set the elements, so defaults aren't observable. Defaults only show through when you allocate by length and read before writing. ## Multidimensional arrays `new int[2][3]` creates a 2-element array whose elements are themselves 3-element `int` arrays, all fully zeroed → all `0`. But `new int[2][]` creates a 2-element array of `int[]` references that are **null** (the inner arrays don't exist yet). The rule is consistent: each created dimension is zeroed by its element type, and a not-yet-created reference dimension is `null`.

  • What does each element of new boolean[5] equal?
    false — boolean's default; the array is zeroed at allocation.
  • What is the difference between new int[2][3] and new int[2][]?
    new int[2][3] is fully allocated and all zeros; new int[2][] allocates only the outer array, whose two int[] elements are null.

saying these in an interview costs you the question

  • Thinking new String[3] holds empty strings rather than nulls
  • Assuming you must fill an array before reading it
  • Believing new int[2][] gives a fully-zeroed 2D grid — inner arrays are null
  • Saying char arrays start with the digit '0'

context