🧱 Arrays · Beginner

Default element values in Java

0, false, '\u0000' or null depending on the element type.

🧩 The mysteryYou create a String array of names and call names[0].length(). It crashes, even though you never put anything bad in there. What's hiding in an empty slot?

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, null
🔮 Predict it

Your 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]);
  1. 0 0.0 false
  2. 0 0 false
  3. 0.0 0.0 false
  4. 0 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'     // 48
🔮 Predict it

Your turn

What prints?

char[] c = new char[1];
System.out.println((int) c[0]);
System.out.println(c[0] == '0');
  1. 0 false
  2. 48 true
  3. 32 false
  4. 0 true
Show the answer

The default char's code is 0. The digit character '0' has code 48, so they're not equal: false.

⚠️ The trap

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(); // 0
🔮 Predict it

Half-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);
  1. Prints 2
  2. Prints 6
  3. Throws NullPointerException
Show the answer

words[0] is "hi", but words[1] is still null, so words[1].length() throws NullPointerException.

💼 In the real world

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

  1. int, long, double... start as 0 or 0.0
  2. boolean starts as false
  3. char starts as '\u0000' (code 0), not a space
  4. String and other objects start as null
🤯 Did you know?

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]);
  1. 0 false null
  2. 0.0 true null
  3. 0.0 false null
  4. 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());
  1. 0
  2. null
  3. Compile error
  4. Throws NullPointerException
Check your answer

Throws NullPointerException. names[0] is null, not "". Calling length() on null throws NullPointerException.

Next: two ways to walk through an array, and how to pick the right one in a second.