Java Quick Reference
  Language Fundamentals
  Operators and Assignments
  Flow Control and Exceptions
  Declarations and Access Control
  Garbage Collection
  Overloading and Overriding
  Threads
  The java.lang Package
  The java.util Package
  The java.awt Package
  The java.io Package
  References
  Miscellaneous Notes
  Tips & Traps
  Mock Exams

Language Fundamentals - Default values

Type Default value
boolean false
byte 0
char '\u0000'
short 0
int 0
long 0l
float 0.0f
double 0.0d
Object null
Array based on Array type

Automatic Initialization

  • Field variables (class members) are automatically initialized to default values
  • Local variables (method or constructor variables) are not automatically initialized
  • Arrays, whether field or local variables, are automatically initialized to the default values of their declared type
    
    class CheckInit {
    
        // field variable
        static int i;                         
        // field array reference variable
        static String[] s = new String[10];   
    
        static void myMethod(){
    
            int j;                // local variable
            int[] a = new int[5]; // local variable array
    
            // causes compile error if not explicitly initialized
            j = 10;     
    
            System.out.println("  Local variable: " + j);
            System.out.println(" Local array ref: " + a[3]);
        }
    
        public static void main(String[] args) {
            System.out.println("Field variable i: " + i);
            System.out.println(" Field array ref: " + s[2]);
            myMethod();
        }
    }
    
    Output of CheckInit:
    
        Field variable i: 0         // default value of int
         Field array ref: null      // default value for String[]
          Local variable: 10        // explicit value
         Local array ref: 0         // default value of int[]
    
    

Timing and duration of variable initializations (JLS §4.5.3)

Variable Type Definition Initialization
Class (Field) Declared with the static keyword within a class or interface Created when the class or interface is prepared.
Automatically initialized to the default value of its type
Duration: as long as the class is loaded
Instance (Field) Declared within a class without the keyword static Created when a new instance is created
Automatically initialized to the default value of its type
Duration: for the life of the instance object
Array components unnamed variables created when an array object is created not when declared intialized to the default value of the array type
Duration: until the array is no longer referenced
Method parameters named argument values passed to a method a new parameter variable is created each time the method is invoked
initialized with the corresponding argument value from the method call
Duration: method execution
Constructor parameters named argument values passed to the constructor a new parameter variable is created each time a new instance is created or the constructor is called
initialized to the corresponding argument value
Duration: construction execution
Exception-handling parameter variables in a catch clause a new exception-handling parameter is created each time an exception is caught by a catch clause
initialized with the actual object associated with the exception
Duration: catch clause execution
Local variables declared by local variable declarations a new local variable is created whenever flow of control enters a new block or for statement
initialized to whatever value is explicitly set within the block or for statement
Duration: execution of the block or for statement

Tips

  • only field variables are automatically initialized to their types default value; local variables must be explicitly initialized
  • arrays are initialized to the default value of their type when they are created, not declared, even if they are local variables

Traps

  • an empty string vs null as the default value for a String object


Source Package Import Class Interface Constructors
  Methods main() Identifiers Keywords Defaults Arrays
  Primitives # Literals char Literal String Literals Class Literals