📦 Variables & Types · Beginner

Default values in Java

Fields get defaults (0, false, null); local variables do not.

🧩 The mysteryCreate an object without setting its fields and they're already 0, false or null. Declare a local the same way and Java refuses to compile. Why?

Fields start zeroed

A field is a variable declared in a class, outside any method. When an object is created, every field gets a default value: the "zero" of its type.

int, long, short, byte  -> 0
double, float           -> 0.0
boolean                 -> false
char                    -> '\u0000'
String, any object      -> null
🔮 Predict it

Your turn

No field is ever assigned. What does this print?

class Player {
    int score;
    boolean active;
    String name;
}
void main() {
    var p = new Player();
    IO.println(p.active);
    IO.println(p.name);
}
  1. false null
  2. Compile error
  3. null null
Show the answer

Fields get defaults: active is false and name is null (and score would be 0). Printing a null reference prints the text null.

Arrays too

A brand-new array is filled with the same defaults, element by element.

String[] names = new String[2]; // null x2
double[] prices = new double[2]; // 0.0 x2
int[] counts = new int[3]; // 0, 0, 0
🔮 Predict it

Your turn

What does this print?

int[] counts = new int[3];
boolean[] flags = new boolean[2];
System.out.println(counts[2]);
System.out.println(flags[0]);
  1. 0 false
  2. null null
  3. Compile error
Show the answer

New array elements start at their type's default: 0 for int and false for boolean.

Locals get nothing

Local variables get no default at all. You must assign one before reading it, and even count++ reads first.

int count;
count++;   // error: might not have
           // been initialized
🤔 Think first

Why the difference?

Why do fields get defaults, but local variables don't?

Think about it, then reveal the answer

A local lives entirely inside one method, so the compiler can easily check every path and force you to be explicit, which catches bugs. A field can be set by constructors or any method, in any order, so Java zeroes it to be safe.

⚠️ The trap

The null field

Printing a null field just prints null, but calling a method on it, like p.name.length(), throws NullPointerException. Default null fields are a classic source of NPEs.

💼 In the real world

In real projects

A new discount field defaults to 0.0, nobody remembers to set it, and every order quietly gets no discount. No crash, just wrong numbers. Good constructors set every field explicitly.

Key takeaways

  1. Numeric fields: 0 or 0.0; boolean: false
  2. Reference fields and array elements: null
  3. char default is '\u0000' (the zero character)
  4. Locals have no default: assign before use
🤯 Did you know?

The char default '\u0000' is the "null character". Printing it usually shows nothing at all, which makes it a sneaky invisible value in output.

Practice questions

What does this print?

class Box {
    int size;
    String label;
}
 
void main() {
    var b = new Box();
    IO.println(b.size);
    IO.println(b.label);
}
  1. 0 ""
  2. Compile error
  3. null null
  4. 0 null
Check your answer

0 null. Fields get default values, so size is 0 and label is null. Printing a null reference prints the text null.

What does this print?

int count;
count++;
System.out.println(count);
  1. 1
  2. 0
  3. Throws NullPointerException
  4. Compile error
Check your answer

Compile error. count is a local variable with no default value. count++ reads it first, so javac reports that it might not have been initialized.

World 3: Operators! Why is 7 / 2 equal to 3, why does x = x++ do nothing, and why isn't 0.1 + 0.2 equal to 0.3?