Skip to content
Related Articles

Related Articles

Default Values Assigned to Primitive Data Types in Java
  • Difficulty Level : Medium
  • Last Updated : 21 Apr, 2021

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 TypeSize
Byte1 byte
Short2 bytes
Int4 bytes
Long8 bytes
Float4 bytes
Double8 bytes
Boolean1 bit
Char1 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 prin 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:

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :