Array Declaration and Initialization in Java
Fast recall
Quick revision
Core answer
Type[] name. Create it with new Type[length], or supply values in an initializer such as int[] scores = {90, 80}. A newly created array is fixed in length and its elements receive defaults: numeric zero, false, the null character for char, and null for reference elements. array.length is a field.Whether you know the three declaration styles, understand how Java allocates array memory on the heap, and know that arrays have fixed size and auto-filled default values.
Confusing the declaration `int[] arr` (no memory yet) with initialization `new int[5]` (memory allocated). Also assuming elements are random garbage values — Java always zero-fills.
Mention that the array reference lives on the stack while the actual block of elements lives on the heap — and that `arr.length` is a field, not a method, so no parentheses.
Interview-ready guide
Interview answer
I declare an array with the component type and brackets, such as int[] scores, and create it with new int[5] or an initializer like {90, 80}. The array length is fixed once it is created, and I read it through the length field.
Java gives new elements default values: zero for numeric types, false for boolean, the null character for char, and null for references. Valid indexes are zero through length - 1. Accessing index length is not a compile-time error; it throws ArrayIndexOutOfBoundsException at runtime.
Array Declaration and Initialization in Java
Direct Answer
Declare an array with Type[] name. Create it with new Type[length], or supply values in an initializer such as int[] scores = {90, 80}. A newly created array is fixed in length and its elements receive defaults: numeric zero, false, the null character for char, and null for reference elements. array.length is a field.
Code Example
Each declaration style fits a different situation, and the snippet below also proves the two facts interviewers probe: Java zero-fills automatically, and length is a field. Run it as a plain Java 17 console app.
public class ArrayDemo {
public static void main(String[] args) {
// Style 1: declare size, fill later
int[] scores = new int[5];
System.out.println(scores[0]); // 0 — Java zero-fills automatically
scores[0] = 95;
scores[1] = 72;
// scores[2], [3], [4] remain 0
// Style 2: declare and initialize with values in one line
String[] days = {"Mon", "Tue", "Wed", "Thu", "Fri"};
System.out.println(days[0]); // Mon
// Style 3: new keyword with explicit values (useful when size is computed)
double[] prices = new double[]{9.99, 14.50, 3.75};
// Length is a field — no parentheses
System.out.println(scores.length); // 5
// System.out.println(scores.length()); // COMPILE ERROR — common mistake
// Default for object arrays is null, not empty string
String[] names = new String[3];
System.out.println(names[0]); // null
}
}
Notice the two traps in one place: scores.length() would refuse to compile, and names[0] is null rather than an empty string — object arrays default to null, not to a "blank" value.
Common Mistakes & Pitfalls
- Writing
arr.length()—lengthis a field on an array, not a method, so the parentheses are a compile error. OnlyStringuseslength(); arrays usearr.length. - Expecting garbage values — beginners coming from C sometimes add code to "clear" a new array first. Java already zero-fills every slot (
0,false,null), so that work is wasted. - Indexing an array you only declared — a field left as
int[] arr;isnull, andarr[0] = 1throwsNullPointerException, not an empty-array error. You must assign withnew(or a literal) before indexing. - Reading index
length— a 5-element array's last valid index is4;arr[arr.length]always throwsArrayIndexOutOfBoundsExceptionbecause indices run0tolength - 1.
Check your understanding
Answer these without looking back at the article.
- What is ArrayIndexOutOfBoundsException and how do you prevent it?
- What is the difference between an array and an ArrayList in Java?
- How does Java store arrays in memory — stack vs heap?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.