skip to content

Default Values

Fields are initialized for you (zero, the null character, false, or null), while locals get nothing and must be definitely assigned before use. Interviewers ask why one compiles and the other does not.

part ofJavaoverview, primer and where to startread it →
on this pageshow

questions

4

When you declare an instance field in a Java class without an initializer, what value does it hold, and how does that depend on its type?

level: juniorimportance: must knowfreq 70%

answer

  1. 0 / 0.0 / false / '' / null
  2. fields default; locals do NOT
  3. locals need definite assignment (compile error)
  4. Integer field → null, int field → 0
  5. JVM zeroes memory at allocation

basics

~10 s

Uninitialized fields get a default automatically: 0 for number types, false for boolean, '' (the null character) for char, and null for any object/reference type. You don't have to set them yourself.

solid answer

~40 s

Instance and static fields that you declare without an initializer are automatically zero-initialized by the JVM. The exact value depends on the type: numeric primitives (byte, short, int, long) default to 0, floating-point types (float, double) to 0.0, char to '' (code point 0, the null character), and boolean to false. Every reference type (objects, arrays, String) defaults to null. This happens during object construction, before your constructor body runs, so a field is never in an undefined state. The same defaulting applies to array elements after the array is created. Local variables are the exception: they get no default and must be definitely assigned before use, or the code won't compile.

go deeper

for a junior

Recall the table: 0 for numbers, false for boolean, '' for char, null for references; and that fields get these automatically while locals don't.

for a middle

Explain that the JVM zeroes memory and that defaults apply to instance fields, static fields, and array elements; distinguish primitive vs reference defaults including the wrapper-null trap.

for a senior

Tie it to definite-assignment analysis for locals, the object initialization order (defaults → initializers → constructor), and why the field-vs-local distinction is a deliberate safety design.

for a principal

Discuss implications for API design and safe object construction (avoid leaking 'this' during construction so others don't observe default-state fields), and the spec guarantees the JVM relies on.

## What a 'field' is In Java a **field** (also called a member variable) is a variable declared directly inside a class but outside any method, like `int count;` or `String name;`. There are two kinds: **instance fields** (one copy per object) and **static fields** (one copy shared by the whole class). This topic is about what value a field holds when you declare it *without* giving it a starting value (no `= something`). ## The rule: fields are default-initialized Unlike some languages (C/C++) where an uninitialized variable contains whatever garbage was previously in that memory, Java **guarantees** every field starts with a well-defined default value. The Java Virtual Machine (JVM) zeroes out the memory for an object when it is allocated. So the moment an object exists, all its fields already have predictable values, even before your constructor runs. ## The defaults, by type A **primitive type** is one of Java's built-in value types (not an object). Their defaults are all 'the zero value' of that type: - `byte`, `short`, `int`, `long` → `0` - `float` → `0.0f`, `double` → `0.0d` - `char` → `''` — this is the Unicode character with code point 0, the 'null character'. It is NOT the digit '0' (which is `'0'`), and it is NOT `null` (a char can never be null). If you print it you usually see nothing or a blank, and `(int) myChar` is `0`. - `boolean` → `false` A **reference type** is anything that is not primitive: every object, every array, `String`, your own classes, wrapper types like `Integer`. Their default is always **`null`**, meaning 'points to no object'. Note this catch: a field of type `Integer` (the wrapper class) defaults to `null`, but a field of type `int` (the primitive) defaults to `0` — mixing these up causes `NullPointerException` when the `null` wrapper is auto-unboxed. ## Where these defaults apply 1. **Instance fields** — set when the object is created. 2. **Static fields** — set when the class is first loaded/initialized. 3. **Array elements** — when you create an array with `new int[3]`, all three slots are `0`; `new String[3]` gives three `null` elements. The defaulting is by the array's element type. ## The big exception: local variables A **local variable** is one declared inside a method, constructor, or block. Local variables get **no** default value. The Java compiler enforces a rule called **definite assignment**: you must assign a value to a local variable along every path that reaches a use of it, otherwise you get a compile error: 'variable X might not have been initialized'. This is a deliberate safety feature — accidentally reading an unset local is almost always a bug, so the language catches it at compile time instead of silently handing you a default. ## Why the difference exists Fields belong to objects that may be constructed in many ways and live a long time, so a guaranteed safe initial state avoids undefined behavior. Locals are short-lived and the compiler can prove whether they were set, so it demands you do so explicitly, which prevents subtle bugs. ## Order of initialization for fields When an object is built: (1) memory is zeroed to defaults, (2) field initializers and instance-initializer blocks run in source order, (3) the constructor body runs. So a default is only ever visible if you haven't given the field an explicit value yet.

  • What value does the boolean default to, and the char?
    boolean defaults to false; char defaults to '' (the null character, code point 0), not the digit '0'.
  • If a field is Integer (wrapper) rather than int, what's its default and what risk does that create?
    It defaults to null. Using it in arithmetic auto-unboxes it, throwing NullPointerException.

saying these in an interview costs you the question

  • Saying char defaults to '0' the digit — it's '', the null character (code point 0)
  • Claiming local variables also get default values — they require explicit assignment
  • Saying a char can be null — char is a primitive and is never null
  • Thinking an Integer/wrapper field defaults to 0 — it defaults to null

context

open as a page

Why does this not compile: `int x; System.out.println(x);` inside a method, even though an int field would print 0?

level: juniorimportance: must knowfreq 65%

basics

~20 s

Local variables don't get default values. The compiler requires you to assign a value before reading one, so it rejects the code with 'variable x might not have been initialized'. A field would instead default to 0.

open as a page

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%

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.

open as a page

Do final fields receive default values, and how does that interact with the requirement that a final field must be assigned exactly once?

level: seniorimportance: should knowfreq 35%

basics

~20 s

A final field is briefly default-initialized (0/false/null) like any field, but the compiler still forces you to assign it exactly once before the constructor finishes. So you can't rely on the default as its final value.

open as a page