Default values in Java
Fields get defaults (0, false, null); local variables do not.
Fields start zeroed
A field is a variable declared in a class, outside any method. When an object is created, every field gets a default value: the "zero" of its type.
int, long, short, byte -> 0
double, float -> 0.0
boolean -> false
char -> '\u0000'
String, any object -> nullYour turn
No field is ever assigned. What does this print?
class Player {
int score;
boolean active;
String name;
}
void main() {
var p = new Player();
IO.println(p.active);
IO.println(p.name);
}false nullCompile errornull null
Show the answer
Fields get defaults: active is false and name is null (and score would be 0). Printing a null reference prints the text null.
Arrays too
A brand-new array is filled with the same defaults, element by element.
String[] names = new String[2]; // null x2
double[] prices = new double[2]; // 0.0 x2
int[] counts = new int[3]; // 0, 0, 0Your turn
What does this print?
int[] counts = new int[3];
boolean[] flags = new boolean[2];
System.out.println(counts[2]);
System.out.println(flags[0]);0 falsenull nullCompile error
Show the answer
New array elements start at their type's default: 0 for int and false for boolean.
Locals get nothing
Local variables get no default at all. You must assign one before reading it, and even count++ reads first.
int count;
count++; // error: might not have
// been initializedWhy the difference?
Why do fields get defaults, but local variables don't?
Think about it, then reveal the answer
A local lives entirely inside one method, so the compiler can easily check every path and force you to be explicit, which catches bugs. A field can be set by constructors or any method, in any order, so Java zeroes it to be safe.
The null field
Printing a null field just prints null, but calling a method on it, like p.name.length(), throws NullPointerException. Default null fields are a classic source of NPEs.
In real projects
A new discount field defaults to 0.0, nobody remembers to set it, and every order quietly gets no discount. No crash, just wrong numbers. Good constructors set every field explicitly.
Key takeaways
- Numeric fields: 0 or 0.0; boolean: false
- Reference fields and array elements: null
- char default is '\u0000' (the zero character)
- Locals have no default: assign before use
The char default '\u0000' is the "null character". Printing it usually shows nothing at all, which makes it a sneaky invisible value in output.
Practice questions
What does this print?
class Box {
int size;
String label;
}
void main() {
var b = new Box();
IO.println(b.size);
IO.println(b.label);
}- 0 ""
- Compile error
- null null
- 0 null
Check your answer
0 null. Fields get default values, so size is 0 and label is null. Printing a null reference prints the text null.
What does this print?
int count;
count++;
System.out.println(count);- 1
- 0
- Throws NullPointerException
- Compile error
Check your answer
Compile error. count is a local variable with no default value. count++ reads it first, so javac reports that it might not have been initialized.