Scrivio Interview library
CodingFoundations7 min read · updated 12 November 2026

What do these print?

Java
int[] a = new int[3];
String[] s = new String[2];
boolean[] f = new boolean[1];
System.out.println(a[0] + " " + s[0] + " " + f[0] + " " + a.length);
int[] b = {};
System.out.println(b.length);

The first line prints 0 null false 3 and the second prints 0. When an array is created, every element gets its type's default value, 0 for int, null for any reference type such as String, and false for boolean, and the length given in brackets is fixed for the array's life. An empty literal {} creates a real array whose length is 0. length is a final field rather than a method, arrays are objects on the heap even when they hold primitives, and new int[-1] compiles but throws NegativeArraySizeException at run time.

Why this question is asked

Arrays are the most basic data structure in Java, and questions about their defaults look easy. They are useful in an interview because the answers explain a lot about how the JVM works: why a field is safe to read before we assign it but a local variable is not, why an array of objects starts full of nulls, and why some mistakes are caught by the compiler while others only fail at run time. In this answer, we will recap what an array is, explain the default values, walk through each printed value, and then look at empty arrays, the length field and the negative size case.

1A Quick Recap of Java Arrays

An array is a fixed-length container of elements that all have the same type. We create one with new, giving its length, or with an initializer such as {1, 2, 3}, from which the compiler works out the length. Once created, the length never changes; a bigger array means a new array and a copy, which is what ArrayList does internally when it grows.

In Java, every array is an object. It lives on the heap, it has a class, and the variable holds a reference to it, just as a String variable holds a reference to a String. That is true even for int[], whose elements are plain ints: the elements are primitives, but the array that holds them is an object.

2Every Element Starts With a Default Value

When the JVM creates an array, it clears the memory for the elements, and cleared memory means zero in the element's type. For numbers that is 0 or 0.0, for char it is the character with code 0, for boolean it is false, and for any reference type it is null, a reference that points to no object.

Default values of array elements
Element typeDefaultExample
byte, short, int, long0new int[3] holds 0, 0, 0
float, double0.0new double[2] holds 0.0, 0.0
char'\u0000', the character with code 0Prints as an invisible character
booleanfalsenew boolean[1] holds false
Any reference typenullnew String[2] holds null, null
The JVM zeroes the memory; each type reads zero in its own way.

3Walking Through the First Line

Now we can read the code. a has three ints, s has two String references and f has one boolean, all at their defaults.

Java
int[] a = new int[3]; // [0, 0, 0]
String[] s = new String[2]; // [null, null]
boolean[] f = new boolean[1]; // [false]
System.out.println(a[0] + " " + s[0] + " " + f[0] + " " + a.length); // 0 null false 3

The expression is evaluated from left to right. a[0] + " " is an int plus a String, so it becomes string concatenation and gives 0 followed by a space. Adding s[0], which is null, appends the four letters null, because string concatenation turns a null reference into the text null rather than throwing. Then false and the length 3 are appended. Notice that only reading s[0] is safe here: calling a method on it, such as s[0].length(), would throw NullPointerException, because there is no String object yet.

A small trap hides in the order of evaluation: if the expression started with a[0] + a[1] + " ", the first plus would add two ints, giving 0, before any string is involved. Concatenation only starts at the first String operand.

4Empty Arrays Are Real Arrays

int[] b = {} creates an array object with no elements, so b.length is 0. It is not null: we can loop over it, pass it around and ask for its length safely. A loop simply runs zero times. Only accessing an element fails, because there is no index that is in range.

Java
int[] b = {};
System.out.println(b.length); // 0
for (int x : b) { } // runs zero times
int first = b[0]; // ArrayIndexOutOfBoundsException: Index 0 out of bounds for length 0
int[] none = null;
int n = none.length; // NullPointerException: there is no array at all

That difference between an empty array and null is why methods that find nothing should return an empty array or collection instead of null: callers can then loop without a null check.

5Length Is a Final Field, Not a Method

Every array has a public final field named length. We read it without parentheses, a.length, unlike String's length() method and a collection's size() method, which is a common slip when switching between them. Because the field is final, we cannot assign to it, and the compiler says so.

Java
int len = a.length; // 3
"abc".length(); // 3, a method on String
List.of(1).size(); // 1, a method on collections
a.length = 5; // compile error: cannot assign a value to final variable length

6A Negative Size Fails at Run Time

The size in new int[n] can be any int expression, such as a value read from a file, so the compiler cannot know whether it is negative. The check happens when the code runs: new int[-1] compiles fine and throws NegativeArraySizeException with the message -1. At the other extreme, a size close to Integer.MAX_VALUE throws OutOfMemoryError, either because the heap is too small or with the message Requested array size exceeds VM limit, since the JVM cannot create arrays of exactly that size.

Java
int n = -1;
int[] bad = new int[n]; // NegativeArraySizeException: -1
int[] huge = new int[Integer.MAX_VALUE]; // OutOfMemoryError: Requested array size exceeds VM limit

So code that sizes arrays from external input should validate the number first, with a clear error message, instead of letting these exceptions escape.

7Follow-up questions

Why do array elements get defaults when local variables do not?

Because the compiler can check local variables but not array elements. A local variable is used only in one method, so the compiler can prove whether it is assigned before it is read, and it rejects the code with variable x might not have been initialized if not. An array's elements are filled at run time, in any order, by any code that has a reference to it, so no such proof is possible. The JVM therefore guarantees a known starting value by clearing the memory, the same rule it applies to object fields.

How do we fill an array with a value other than the default?

Arrays.fill(a, -1) sets every element to -1, and Arrays.fill(a, from, to, value) does part of it. Arrays.setAll(a, i -> i * i) computes each element from its index. For a quick literal, an initializer such as int[] a = {1, 2, 3} is clearest. Note that for an array of objects, Arrays.fill(people, new Person()) puts the same single object in every slot, which is rarely what we want; setAll with a lambda that creates a new object per index is usually correct.

Is new int[0] expensive, and can we reuse it?

It is cheap, but it still creates an object each time. Because an empty array has no elements, it can never be changed, so it is safe to share one instance, for example private static final int[] EMPTY = new int[0], and return it whenever there are no results. The JDK and many libraries do exactly that.

8Conclusion

In this answer, we saw that every array element starts with its type's default, 0 for int, null for references and false for boolean, so the first line prints 0 null false 3, and that the empty literal {} creates a real array of length 0. Arrays are heap objects with a fixed length, exposed as a final field rather than a method, reading a null element is safe while calling a method on it is not, and a negative size such as new int[-1] compiles but throws NegativeArraySizeException at run time.

What to say in the interviewAbout 100 seconds aloud

Open with

It prints 0 null false 3, and then 0. When you create an array, the JVM clears its memory, so every element starts at its type's default: zero for numeric types, null for references like String, false for boolean, and the zero character for char.

Then, if they want more

So a[0] is 0, s[0] is null, and f[0] is false, and a.length is 3 because that's the size we asked for. String concatenation turns the null into the text null instead of throwing; it's only calling a method on s[0] that would throw a NullPointerException.

The second line prints 0 because an empty initializer creates a real array object with length zero; it's not null, so you can loop over it safely, and only accessing an element throws ArrayIndexOutOfBoundsException.

A few related points: length is a public final field, so it has no parentheses and you can't assign to it, unlike String's length method or a collection's size. Arrays are always objects on the heap, even int arrays; the elements are primitives but the container is an object.

Their length is fixed, so growing means copying, which is what ArrayList does. And the size is only checked at run time, so new int[-1] compiles fine and throws NegativeArraySizeException.

That's also why array elements get defaults while local variables don't: the compiler can prove a local is assigned before use, but it can't track array elements, so the JVM guarantees a starting value.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "What do these print?": https://scrivio-interview-library.netlify.app/java/arrays/6c968af81d/