Default Variable Initialization in Java

Default Variable Initialization in Java is to initialize to a default value when they are declared. The default value of a variable depends on its data type.

The following table shows the default values of variables in Java:

Data TypeDefault Value
byte0
short0
int0
long0L
float0.0f
double0.0d
char‘\u0000’
booleanfalse
Objectnull

Note that the default value of an object reference is null. This means that an object reference that has not been initialized refers to no object.

If a variable is declared without an initial value, it will be initialized to its default value. For example, the following code declares a variable named age of type int without an initial value:

int age;

The variable age will be initialized to the default value of int, which is 0.

It is important to note that local variables must be initialized before they can be used. If a local variable is not initialized, the compiler will generate an error.

For example, the following code will generate an error because the local variable age is not initialized:

public class HelloWorld {
    public static void main(String[] args){
        int age;
        System.out.println(age); 
    }
}

When we compile the above code it will display the following error:

HelloWorld.java:4: error: variable age might not have been initialized
        System.out.println(age);
                           ^
1 error

Frequently Asked Questions

What is the default value for an uninitialized integer variable in Java?

The default value for an uninitialized integer variable in Java is 0.

What is the default value for an uninitialized floating-point variable in Java?

The default value for an uninitialized floating-point variable (float or double) in Java is 0.0.

What is the default value for an uninitialized boolean variable in Java?

The default value for an uninitialized boolean variable in Java is false.

What is the default value for an uninitialized character variable in Java?

The default value for an uninitialized character variable (char) in Java is the null character '\u0000'.

What is the default value for an uninitialized object reference variable in Java?

The default value for an uninitialized object reference variable in Java is null.