When you create an array with new, what initial values do its elements hold?
answer
- Numbers→0, boolean→false, char→'', refs→null
- Java zeroes everything; never garbage (unlike C)
- new String[n] = all null (NPE trap)
- Same rule as fields, not local variables
- Jagged new int[3][] = null rows
basics
~10 sEvery element is automatically set to a default: 0 for numbers, false for boolean, the null character for char, and null for objects. So new int[3] is [0,0,0] and new String[3] is three nulls.
solid answer
~40 sUnlike C, Java guarantees that `new` zeroes out every array element to its type's default — there is never uninitialized garbage. The defaults are: `0` for `byte/short/int/long`, `0.0`/`0.0f` for `double/float`, `false` for `boolean`, the null character `''` for `char`, and `null` for any reference type. This is the same rule that applies to instance fields, and it's why `new int[5]` is immediately usable as five zeros while `new String[5]` is five nulls you must populate before use. The guarantee applies at creation time only; once you overwrite a slot it holds whatever you put there. Note the initializer shorthand (`{1,2,3}`) sets explicit values instead, but any slot you don't list in a sized+filled scenario still defaults — though Java doesn't let you mix a size and an initializer.
go deeper
Knows numeric arrays start at 0 and object arrays start as null.
Lists all the per-type defaults including char '' and false, and recognizes the new String[] null trap.
Ties the rule to the field-vs-local distinction and explains jagged-array null rows and multi-dim allocation.
Explains the JLS/JVM zeroing guarantee, its memory-safety rationale (no information leakage from reused memory), and contrasts it with C's undefined behavior.
## The guarantee When you allocate an array with `new`, the JVM **zero-initializes every element** to the default value of the element type. This is a hard language guarantee — there is no such thing as a Java array slot holding undefined/garbage memory (a sharp contrast with C, where `int a[5];` leaves whatever was on the stack). ## The default per type | Element type | Default value | |---|---| | `byte`, `short`, `int`, `long` | `0` (`0L` for long) | | `float`, `double` | `0.0f` / `0.0` | | `char` | `''` (the null character, code point 0) | | `boolean` | `false` | | any reference type (objects, `String`, arrays) | `null` | So: ```java new int[3] // {0, 0, 0} new boolean[2] // {false, false} new char[2] // {'', ''} (prints as blanks) new String[2] // {null, null} new int[2][2] // {{0,0},{0,0}} (inner arrays are created too) ``` ## Why this matters - **Reference arrays are full of nulls.** `new Person[10]` gives ten `null`s, not ten `Person` objects. Iterating and calling a method on each without first assigning real objects throws `NullPointerException`. This is one of the most common beginner traps. - **Numeric arrays are ready to accumulate.** `new int[n]` can be summed/incremented immediately because every slot is `0`. - **char defaults are invisible.** `''` prints as nothing, so a `char[]` can look empty even though it is full of null characters. ## Where the rule comes from It is the same default-value rule the JLS applies to **instance and static fields**. Only **local variables** lack default initialization (the compiler forces you to assign them before use). Array elements behave like fields, not locals — hence the auto-defaults. ## Multi-dimensional nuance `new int[3][4]` creates the outer array *and* allocates each inner `int[4]`, all zeroed. But `new int[3][]` (jagged) creates the outer array with three `null` inner-array references — the rows themselves are `null` until you create them. So the 'default' of an unallocated inner dimension is `null`, consistent with reference-type defaults. ## Interaction with initializers The `{...}` initializer assigns explicit values rather than relying on defaults; and because Java forbids combining an explicit size with an initializer, you never have a 'partially specified' sized array — it's either fully defaulted (`new int[n]`) or fully listed (`{...}`). ## Bottom line `new` always returns an array whose every slot holds the type's default: numeric `0`, `false`, `''`, or `null`. No garbage, ever — but remember reference arrays start as all-null.
- What does new Person[3] contain right after creation?Three null references, not three Person objects. You must assign a real Person to each slot before using it, or you'll get a NullPointerException.
- Why don't local variables get the same default treatment as array elements?The JLS only auto-initializes fields and array elements. Locals are deliberately left undefined and the compiler enforces definite assignment, catching 'used before set' bugs at compile time.
saying these in an interview costs you the question
- Thinking new String[5] contains empty strings instead of nulls
- Assuming char[] defaults to space ' ' rather than ''
- Believing array slots can hold uninitialized garbage like in C
- Forgetting inner rows of a jagged array start as null