Default Values of Primitive Data Types in Java
In Java, when primitive data type variables are declared as instance or class-level variables (not local), and no explicit value is assigned, the Java Virtual Machine (JVM) automatically provides them with default values.
These defaults ensure variables always have a predictable value, preventing uninitialized memory issues.
| Data Type | Size | Default Value | Example Use |
|---|---|---|---|
| byte | 1 byte | 0 | Small counters, file handling |
| short | 2 bytes | 0 | Memory-saving integers |
| int | 4 bytes | 0 | General-purpose integers |
| long | 8 bytes | 0L | Large integer values |
| float | 4 bytes | 0.0f | Decimal numbers (low precision) |
| double | 8 bytes | 0.0d | Decimal numbers (high precision) |
| char | 2 bytes | '\u0000' (null character) | Unicode characters |
| boolean | 1 bit (JVM dependent) | false | True/False values |
Example in Java
public class DefaultValuesExample {
byte b;
short s;
int i;
long l;
float f;
double d;
char c;
boolean flag;
public static void main(String[] args) {
DefaultValuesExample obj = new DefaultValuesExample();
System.out.println("byte default: " + obj.b);
System.out.println("short default: " + obj.s);
System.out.println("int default: " + obj.i);
System.out.println("long default: " + obj.l);
System.out.println("float default: " + obj.f);
System.out.println("double default: " + obj.d);
System.out.println("char default: [" + obj.c + "]");
System.out.println("boolean default: " + obj.flag);
}
}
Output:
byte default: 0
short default: 0
int default: 0
long default: 0
float default: 0.0
double default: 0.0
char default: []
boolean default: false
Explanation:
- Java automatically assigns default values only to instance variables (class-level).
- Local variables (inside methods) do not get default values — they must be initialized before use.
- char default value is '\u0000' (null character different from null keyword or Unicode value 0, invisible), which looks like an empty space.
- boolean defaults to false, meaning “not true” until explicitly set.
- Default values help avoid garbage/uninitialized memory issues (common in C/C++).
Summary:
- Default values are assigned only to instance/class variables, not local variables.
- Helps prevent uninitialized memory problems.
- Each primitive type has a fixed predictable default (e.g., numeric → 0, boolean → false, char → '\u0000').