Course contentsShow
← Java
Lesson 237 of 2,8706. ArraysFree lesson

Default Initialization Values

Learn what default values Java assigns to array elements: 0 for numbers, false for boolean, null for objects.

Default Initialization Values

What you'll learn: When you create an array in Java, its elements automatically receive default values based on their type—you don't start with garbage data.

The Concept

Unlike local variables (which you must initialize before use), array elements are automatically initialized the moment you create the array with new. Java gives each element a sensible "zero-like" default value depending on the element's type.

Think of it like this: when you reserve hotel rooms, they come cleaned and ready with standard amenities—not left dirty from the previous guest. Similarly, Java ensures your array "rooms" start in a predictable, safe state.

Default Values by Type

Here's what Java assigns automatically:

  • Numeric types (int, double, byte, short, long, float): 0 (or 0.0)
  • boolean: false
  • Reference types (objects, including String): null
  • char: '\u0000' (the null character, essentially zero)

Examples

int[] numbers = new int[3];
// All elements are 0: [0, 0, 0]

boolean[] flags = new boolean[2];
// All elements are false: [false, false]

String[] names = new String[4];
// All elements are null: [null, null, null, null]

You can immediately read these values—they're valid, just "empty" starting points. If you had a local variable int x; you couldn't use x without assigning it first, but array elements are different: they're ready to use right away.

Why This Matters

Default initialization prevents unpredictable behavior. You always know the starting state of your array, making your code safer and more predictable.

Key Takeaway: Java automatically initializes array elements to type-specific defaults: 0 for numbers, false for booleans, and null for objects—no manual initialization required.