Open In App

Default Values Assigned to Primitive Data Types in Java

Improve
Improve
Like Article
Like
Save
Share
Report

In Java, when a variable is declared but not initialized, it is assigned a default value based on its data type. The default values for the primitive data types in Java are as follows:

byte: 0
short: 0
int: 0
long: 0L
float: 0.0f
double: 0.0d
char: ‘\u0000’ (null character)
boolean: false

It is important to note that these default values are only assigned if the variable is not explicitly initialized with a value. If a variable is initialized with a value, that value will be used instead of the default.

Primitive data types are built-in data types in java and can be used directly without using any new keyword. As we know primitive data types are treated differently by java cause of which the wrapper class concept also comes into play. But here we will be entirely focussing on data types. So, in java, there are 8 primitive data types as shown in the table below with their corresponding sizes.

Data Type Size
Byte 1 byte
Short 2 bytes
Int 4 bytes
Long 8 bytes
Float 4 bytes
Double 8 bytes
Boolean 1 bit
Char 1 byte

Now, here default values are values assigned by the compiler to the variables which are declared but not initialized or given a value. They are different according to the return type of data type which is shown below where default values assigned to variables of different primitive data types are given in the table. However, relying on such default values is not considered a good programming style.

Data Type

Default Values

Byte

0

Short

0

Int

0

Long

0

Float

0.0

Double

0.0

Boolean

false

Char

\u0000′ or null

Now as we know Initializing a variable means to give an initial value to a variable before using it. So, in order to use the default values first, declare the variable with data type and name (eg, int x, here int is the data type and x is the name of the variable), if you don’t declare the variable before using it, it would result in a compile-time error. Now to use the default value of the variable do not initialize it, i.e. do not assign a value to it.

Example 1

Java




// Java Program to Print Default Value Assigned
// to Primitive Datatype
 
// Importing input output classes
import java.io.*;
 
// Main class
public class GFG {
 
    // Global class variable
    static int a;
 
    // Main driver method
    public static void main(String[] args)
    {
        // Trying to print the default value
        // assigned to variable
        System.out.println(a);
    }
}


Output

0

Output explanation:

Here ‘a‘ is a class member variable or you can say an instance variable and it will be initialized to its default value by the compiler.

Note: There would have been a problem if variable (‘a’) was not a class member as the compiler never assigns default values to an uninitialized local variable.

In this scenario, there will be an error pointing to variable ‘a‘ that variable ‘a‘ might not have been initialized yet. This is because here ‘a‘ is the main() method local variable and has to be initialized before being used. The compiler never assigns default values to an uninitialized local variable. If you haven’t initialized the variable where you have declared it, assign the variable a value before using it, or else it will result in a compile-time error.

It is as shown in the next example as shown below for a better understanding of the class variable.

Example 2

Java




// Java Program to Print Default Value Assigned
// to Primitive Datatype
 
// Main class
public class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
        // Declaring class member variable
        // (inside a local scope)
        int a;
 
        // Trying to printing the default value assigned
        System.out.println(a);
    }
}


Output:



Last Updated : 24 Mar, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads