Default Values
In Java, different types of variables have different default values when they are not explicitly initialized. Here are the default values for each type:
Primitive Data Types
byte
,short
,int
,long
:0
float
,double
:0.0
char
:'\u0000'
(null character)boolean
:false
Reference Data Types
For reference types (objects), the default value is
null
, meaning they do not refer to any object in memory.
Local Variables
Local variables must be explicitly initialized before they are used. They do not have default values. Compilation error will occur if not initialized and tried to use.
Static and Instance Variables
If they are not explicitly initialized, they are automatically initialized with their default values. Non-static - Non local variables will also be initialized.
Last updated