Skip to content

Default values of primitives

In Java, fields (instance and static variables) are automatically initialized with default values if you don’t assign one. Local variables (inside methods) are not initialized automatically – you must assign a value before use.

TypeDefault value
byte0
short0
int0
long0L
float0.0f
double0.0d
char\u0000 (null character)
booleanfalse

Example:

public class DefaultDemo {
static int staticInt; // default 0
boolean boolField; // default false
char charField; // default '\u0000'
public static void main(String[] args) {
int localInt; // no default, must initialize
// System.out.println(localInt); // compilation error
}
}

For reference types (objects, arrays), the default value is null.

String str; // default null

Important: Always initialize local variables before using them. The compiler enforces this.

public void method() {
int x;
if (condition) {
x = 10;
}
// System.out.println(x); // error: variable might not have been initialized
}