Course contentsShow
← Java
Lesson 280 of 2,8707. Reference Types and Object FundamentalsFree lesson

Default Values: Primitives vs References

Learn that uninitialized primitives get zero/false while reference variables default to null.

Default Values: Primitives vs References

What you'll learn: You'll discover what happens when you don't explicitly initialize primitive and reference variables in Java.

The Automatic Safety Net

When you declare class-level or array variables in Java without assigning them a value, Java doesn't leave them in an undefined state. Instead, it automatically gives them default values—but these defaults differ dramatically between primitives and references.

Primitives: Zero and False

Primitive variables get sensible "zero" defaults:

  • Numeric types (int, double, long, etc.) default to 0 or 0.0
  • boolean defaults to false
  • char defaults to '\u0000' (the null character)

Think of this as Java saying, "If you didn't specify a number, I'll assume zero. If you didn't specify true/false, I'll assume false."

References: null

Reference variables (any object type, including arrays and String) default to null.

null means "this reference points to nothing"—there's no object on the heap. It's like an empty parking space: the spot exists, but no car is parked there.

int[] numbers = new int[3];
// Each element is 0, not random garbage

String[] words = new String[3];
// Each element is null, not empty strings

Notice the difference: an array of int contains actual zeros you can safely use. An array of String contains null references—attempting to use them (like calling .length()) will cause a NullPointerException.

Why This Matters

You must initialize references before using them, or check for null. Primitives are ready to use immediately with their zero values.

Key Takeaway: Uninitialized primitives default to zero/false; uninitialized references default to null, which cannot be dereferenced without error.