Default Values Assigned to Primitive Data Types in Java
Last Updated :
01 Oct, 2024
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
Note: 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 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:

Similar Reads
Java Program to Convert Int to Double Java data types can be categorized as primitive and non-primitive. Primitive data types contain a single value, whereas non-primitive data types contain an address of the variable value. Java supports 7 primitive data types - boolean, byte, char, short, int, long, float, and double. These data types
4 min read
Which Data Type Cannot be Stored in Java ArrayList? The ArrayList class implements a growable array of objects. ArrayList cannot hold primitive data types such as int, double, char, and long. With the introduction to wrapped class in java that was created to hold primitive data values. Objects of these types hold one value of their corresponding prim
4 min read
Local Variable Type Inference or LVTI in Java 10 In Java, type inference refers to the automatic detection of the datatype of a variable, done generally at compiler time. In this article, we are going to discuss about local variable type inference in Java in detail.What is Local Variable Type Inference (LVTI)?Local variable type inference is a fea
5 min read
Creating a Generic Array in Java Arrays in Java are generated using a certain data type. On the other hand, you may create a generic array that functions with various object types by utilizing generics. You can build type-safe, reusable code by using generics. In this article, we will learn about creating generic arrays in Java.Jav
2 min read
Primitive data type vs. Object data type in Java with Examples Data Types in Java Every variable in java has a data type. Data types specify the size and type of values that can be stored in an identifier. Java language is rich in its data types. The variety of data types available allow the programmer to select the type appropriate to the need of the applicati
6 min read
Comparison of double and float primitive types in Java Consider the following two codes in Java: Java // This program prints true class Geeksforgeeks { public static void main(String args[]) { float f = 5.25f; double d = 5.25 System.out.println(f == d); } } Output true Java // But this program prints false. class Geeksforgeeks { public static void main(
2 min read