Default Values Assigned to Primitive Data Types in Java - GeeksforGeeks

29 Oct, 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.
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); 
    } 
}






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:
sweetyty

java-basics


Java-Data Types


Java

Writing code in comment? 
                                    Please use ide.geeksforgeeks.org, 
                                    generate link and share the link here.
                                
Functional Interfaces in Java
Stream In Java
Constructors in Java
Different ways of Reading a text file in Java
Exceptions in Java
Generics in Java
Comparator Interface in Java with Examples
Strings in Java
Difference between Abstract Class and Interface in Java
How to remove an element from ArrayList in Java?