Default Values of Data Types

Naveen AnamaneniUpdated · 4 min read

When a new bank account is opened, the balance starts at zero. Nobody types the zero in; it's the sensible starting value. Java does the same for some variables: if you don't give a field or an array element a value, Java fills in a default value. But for local variables inside a method, it deliberately refuses, and that difference trips up many beginners.

Who Gets a Default, and Who Doesn't

Fields and array elements get defaults; local variables don't
Where the variable isDefault value?
Instance variable (field)Yes
Static variableYes
Array elementYes
Local variable (inside a method)No: must be assigned before it's read

The Default for Each Type

TypeDefault value
byte, short, int0
long0L
float0.0f
double0.0
char'\u0000' (the "null character", code 0)
booleanfalse
Any reference type (String, arrays, objects)null

See the Defaults

Ticket.java▶ Try it
public class Ticket {
    int seat;
    double fare;
    boolean paid;
    char coach;
    String passenger;
 
    public static void main(String[] args) {
        Ticket t = new Ticket();
        System.out.println("seat: " + t.seat);
        System.out.println("fare: " + t.fare);
        System.out.println("paid: " + t.paid);
        System.out.println("coach code: " + (int) t.coach);
        System.out.println("passenger: " + t.passenger);
 
        int[] counts = new int[3];
        System.out.println("counts: " + java.util.Arrays.toString(counts));
    }
}
Output
seat: 0
fare: 0.0
paid: false
coach code: 0
passenger: null
counts: [0, 0, 0]

How it works

  1. Lines 2 to 6 declare five fields without giving them values.
  2. Line 9 creates a Ticket object, and Java fills every field with its default.
  3. Line 13 casts the char to int to show its code, 0. Printed as a character, '\u0000' is invisible.
  4. Line 16 creates an array of three ints. Each element starts at 0, and Arrays.toString on line 17 prints them all.

Try it yourself: add a field long pnr; and print it. Then make the array a String[] instead of int[]. What do the elements start as?

Why Locals Get No Default

Inside a method, Java insists that you assign a variable before reading it:

Compiler output
Errs6.java:4: error: variable x might not have been initialized
        System.out.println(x);
                           ^

This is on purpose. A local variable that you forgot to set is almost always a bug. A silent 0 would hide it; a compile error points straight at it. Fields are different: an object may legitimately be created before all its fields are known, so Java gives them safe starting values.

Common Mistakes

Expecting a local int to start at 0. It doesn't compile. Write int total = 0;.

Comparing a default char with a space. '\u0000' is not ' '. The space character is code 32.

Assuming a new String[] contains empty strings. Its elements are null, not "".

Practice

  1. Easy: Write the default value for each: an int field, a boolean array element, a String field, a double static variable, a local int.
  2. Harder: Create a boolean[] seatsBooked = new boolean[5];, mark seats 1 and 3 as booked, and print all five values.

Quick check: Default values

4 questions
1What is the default value of an int field?
2What is the default value of a String field?
3What happens if you print a local int that was never assigned?
4What are the elements of new boolean[4]?

Key Takeaways

Was this tutorial helpful?

Lesson 35 of 482