Distinguish null references, default field values, allocated arrays, and definitely assigned local variables.
Answer: Its instance fields first receive type-based default values. Field initializers, initializer blocks, and constructor processing then establish the intended state according to the language's initialization order.
Each object has its own instance-field variables. These defaults are runtime behavior, not assignments performed when the Java source is compiled.
Answer: Byte, short, int, and long default to zero; float and double to positive zero; boolean to false; char to the zero UTF-16 code unit; and references to null.
The char default is not a space. Printing it directly may appear blank because the zero code unit is not an ordinary visible character. Casting it to int reveals its numeric value, 0.
Answer: It starts at null. Constructor or initializer code may later assign it a reference. This rule applies to fields, not to reading an unassigned local variable.
A reference field is sometimes called an object variable, but its value is a reference rather than the object itself. A final field also has separate definite-assignment requirements for its intended initialization.
Answer: Null is a reference value that identifies no object. An object itself is not "a null object"; a variable or expression may instead have the null reference value.
Null can be assigned to reference types, not to primitive variables. Dereferencing null for an instance field or method, or unboxing a null wrapper, throws NullPointerException.
Answer: Compare it with null, as in if (title != null) { ... }. The variable must already have been assigned. Such a check establishes a fact about that value; it does not prove the object was created at that particular line.
For case conversion independent of a user's locale, use title.toLowerCase(java.util.Locale.ROOT) when appropriate. Check the receiver before calling a method on it; a returned title being checked does not protect an earlier null receiver used to obtain it.
Answer: It is null, because an array field stores a reference. Declaring the field does not allocate an array. Calling its length or indexing it before assigning an array causes NullPointerException.
The same default applies to static array fields. A local array variable, however, must be definitely assigned before use; there is no automatic local null default.
Answer: Yes. An allocation such as new int[3] creates three elements initialized to zero. Reference-array elements start at null, regardless of whether the variable holding the array is a field or a local.
An array initializer supplies explicit element values as part of creating the array. Allocation and element defaults are separate from merely declaring an array reference variable.
Answer: No. new StringBuilder[2] creates an array with two null reference elements. It does not create two StringBuilder instances.
public class DefaultState {
private int number;
private boolean active;
private char character;
private String[] missing;
public static void main(String[] args) {
DefaultState state = new DefaultState();
System.out.println(state.number + " " + state.active + " " + (int) state.character);
System.out.println(state.missing == null);
StringBuilder[] builders = new StringBuilder[2];
System.out.println(builders[0] == null);
builders[0] = new StringBuilder("ready");
System.out.println(builders[0]);
}
}
This prints 0 false 0, true, true, and ready. Only the explicitly assigned element receives a builder reference.
Answer: The older term refers to storage managed with execution of the surrounding scope rather than explicit allocation by the programmer. Local variable is clearer Java terminology.
Leaving a block ends the variable's scope, but does not necessarily destroy an object that it referenced. Another reference or a captured value can keep that object reachable, and garbage collection is not tied to immediate block exit.
Answer: It must be definitely assigned before it is read. Assignment may occur in the declaration or later, provided the compiler can establish it on all relevant paths.
This applies to primitive and reference locals alike. The default-zero rule for numeric fields and array elements does not supply a value for an unassigned local int.
References: JLS: initial values, JLS: arrays, and JLS: definite assignment.