Default element values in Java
0, false, '\u0000' or null depending on the element type.
Pre-filled boxes
When you create an array with new, every slot is filled with its type's default value: 0 for int and long, 0.0 for double, false for boolean, '\u0000' for char, and **null** for any object type, like String. (Local variables are different: they get no default at all.)
int[] n = new int[2]; // 0, 0
double[] d = new double[2]; // 0.0, 0.0
boolean[] f = new boolean[2]; // false, false
String[] s = new String[2]; // null, nullYour turn
What prints?
long[] l = new long[1];
double[] d = new double[1];
boolean[] b = new boolean[1];
String out = l[0] + " " + d[0];
System.out.println(out + " " + b[0]);0 0.0 false0 0 false0.0 0.0 false0 0.0 null
Show the answer
A long defaults to 0, a double to 0.0 (printed with the decimal point), and a boolean to false.
char's invisible zero
The default char is '\u0000', the character with code 0. It's not a space (code 32) and not the digit '0' (code 48). It usually prints as nothing visible at all.
char[] c = new char[1];
(int) c[0] // 0
(int) ' ' // 32
(int) '0' // 48Your turn
What prints?
char[] c = new char[1];
System.out.println((int) c[0]);
System.out.println(c[0] == '0');0 false48 true32 false0 true
Show the answer
The default char's code is 0. The digit character '0' has code 48, so they're not equal: false.
null is not empty
A new String[3] holds **three nulls, not three empty strings ""**. Calling a method on a null slot, like names[0].length(), throws NullPointerException. Fill the array first, or check for null.
String[] names = new String[3];
names[0].length(); // NullPointerException
Arrays.fill(names, ""); // now all ""
names[0].length(); // 0Half-filled
Only one slot was filled. What happens?
String[] words = new String[2];
words[0] = "hi";
int a = words[0].length();
int b = words[1].length();
System.out.println(a + b);Prints 2Prints 6Throws NullPointerException
Show the answer
words[0] is "hi", but words[1] is still null, so words[1].length() throws NullPointerException.
In real projects
Half-filled object arrays are a classic source of NullPointerException in production: a lookup table where some entries were never set, or a batch where one record failed to load. Defensive code fills arrays up front or checks for null before calling methods.
Key takeaways
- int, long, double... start as 0 or 0.0
- boolean starts as false
- char starts as '\u0000' (code 0), not a space
- String and other objects start as null
Tony Hoare introduced the null reference in 1965 and later called it his "billion-dollar mistake", because of all the crashes it has caused since.
Practice questions
What does this print?
double[] d = new double[2];
boolean[] b = new boolean[2];
String[] s = new String[2];
System.out.println(d[0] + " " + b[1] + " " + s[0]);- 0 false null
- 0.0 true null
- 0.0 false null
- 0.0 null null
Check your answer
0.0 false null. A double defaults to 0.0 (printed with the decimal), a boolean to false, and a String element to null, which string concatenation prints as null.
What does this print?
String[] names = new String[3];
System.out.println(names[0].length());- 0
- null
- Compile error
- Throws NullPointerException
Check your answer
Throws NullPointerException. names[0] is null, not "". Calling length() on null throws NullPointerException.