Java: Default values

The default value of an object is null. For primitive data types the default values are as follows: numerical types become 0, a char has the Unicode character for zero (\0000) and a boolean is false.

Default values will be automatically assigned by the compiler, but only for fields and not for local variables.

package javaapplication23;

public class PrintInt {

    int x;  // field is assigned default value 0

    public static void main(String[] args) {

        int y = printInt();

        System.out.println(y);

        PrintInt p = new PrintInt();
        System.out.println(p.x);
    }

    static int printInt() {

        int x; // local variable must be assigned if used

        x = 10;
        return x;
    }
}

default

Series Navigation<< Java: Null object
Java: Garbage collector >>