skip to content

Array Declaration and Creation

The declaration variants, the new and literal forms, the zero, null or false defaults for elements, and length as a fixed field rather than a method. Small details that show up in screening questions.

part ofJavaoverview, primer and where to startread it →
on this pageshow

questions

5

What are the ways to declare an array variable in Java, and is there a difference between int[] a and int a[]?

level: juniorimportance: must knowfreq 70%

answer

  1. Brackets on type vs. brackets on name
  2. int[] a,b → both arrays; int a[],b → only a is array
  3. Declaration ≠ allocation; reference is null/unassigned
  4. Type-side form is the Java convention
  5. Multi-dim: int[][] grid

basics

~20 s

You can write the brackets after the type, like int[] a, or after the variable name, like int a[]. Both declare an array of ints and behave identically. The first style is preferred in Java.

solid answer

~40 s

Java accepts two declaration forms: int[] a (brackets on the type) and int a[] (brackets on the variable, a C-style holdover). They are equivalent for that variable. The type-side form int[] is the strongly recommended Java convention because it reads as 'a is an array of int', and it avoids a subtle trap: in int[] a, b; both a and b are int arrays, but in int a[], b; only a is an array and b is a plain int. Declaring a variable does not create an array object; it only creates a reference that is null until you assign an actual array. You can also stack brackets for multi-dimensional arrays (int[][] grid).

code

java · 3 lines
java
int[] a, b;   // both a and b are int arrays (preferred)
int c[], d;   // c is int[], but d is a plain int (the trap)
int[][] grid; // 2-D array reference

go deeper

for a junior

States both forms exist and that int[] a is the preferred style; knows declaration alone makes no elements.

for a middle

Explains the multi-variable trap (int a[], b;) and why the type-side form prevents it.

for a senior

Connects declaration to reference semantics and null state, and applies the convention to parameters/returns and varargs.

for a principal

Frames it as a readability/consistency standard worth enforcing via linters, and explains the historical C-compatibility rationale.

## What an array is An **array** in Java is an object that holds a fixed number of values of the same type, stored in contiguous, indexed slots. Indices run from 0 to length-1. Arrays are reference types: an array *variable* holds a reference (a pointer) to the array object on the heap, not the elements themselves. ## Declaration vs. creation **Declaring** an array variable means naming it and giving it a type. It does NOT allocate any storage for elements. Until you assign an array object, the variable is `null` (for a local variable it is simply unassigned and the compiler forbids reading it). **Creating** (instantiating) the array — allocating the slots — is a separate step done with `new` or an initializer (covered in other questions). ## The two syntax forms Java, for C/C++ familiarity, allows the square brackets in two positions: 1. **Type-associated (recommended):** `int[] a;` — read as "a is an int array". 2. **Variable-associated (legacy/C-style):** `int a[];` — read the C way, "a[] is an int". For a single variable these are 100% equivalent; the compiler treats `a` as `int[]` either way. ### Why the type-side form is preferred The difference becomes visible (and dangerous) when you declare multiple variables on one line: - `int[] a, b;` → **both** `a` and `b` are `int[]` (arrays). The `[]` binds to the type, so it applies to every name. - `int a[], b;` → `a` is `int[]` but `b` is a plain `int`. The `[]` binds only to `a`. The second form silently produces a non-array `b`, a classic bug. Java's official style guide and most codebases therefore mandate the type-side form, and you can even put brackets in a third (discouraged) hybrid spot on method return types. ## Multi-dimensional and mixed forms For 2-D arrays you stack brackets: `int[][] grid;`. The brackets may legally be split across both positions (`int[] x[];` is the same as `int[][] x;`), but this is confusing and never recommended. ## Method parameters and returns The same rule applies to parameters: `void f(int[] a)` is preferred over `void f(int a[])`. A varargs parameter `int... a` is internally an `int[]`. ## Bottom line Use `int[] a;`. It is unambiguous, scales correctly to multiple declarations, and matches Java convention. The `int a[]` form is legal but a code smell.

  • In the line int a[], b; what is the type of b?
    A plain int, not an array. The brackets bind only to a, which is why the type-side form (int[] a, b;) that makes both arrays is preferred.
  • Does int[] a; allocate any memory for elements?
    No. It only declares a reference. The reference is null (for fields) until you assign an array via new or an initializer; reading an unassigned local is a compile error.

saying these in an interview costs you the question

  • Believing int a[] and int[] a differ for a single variable (they don't)
  • Thinking declaring an array allocates its elements
  • Claiming int a[], b; makes both arrays
  • Using the C-style form in new Java code

context

open as a page

How do you get the size of an array in Java, and why is it a field rather than a method?

level: juniorimportance: must knowfreq 68%

basics

~10 s

You read the public length field, like a.length (no parentheses). It tells you how many elements the array has. It is fixed for the array's lifetime and you cannot change it.

open as a page

How do you create (instantiate) an array in Java with the new keyword, and what does the size argument do?

level: juniorimportance: must knowfreq 75%

basics

~20 s

Use new with the element type and a length, like new int[5]. This allocates an array of 5 ints on the heap. The length is fixed for the array's lifetime, and you fill the slots by index afterwards.

open as a page

When you create an array with new, what initial values do its elements hold?

level: middleimportance: must knowfreq 65%

basics

~10 s

Every element is automatically set to a default: 0 for numbers, false for boolean, the null character for char, and null for objects. So new int[3] is [0,0,0] and new String[3] is three nulls.

open as a page

What is the array initializer shorthand in Java, and when can you use the curly-brace form?

level: middleimportance: should knowfreq 60%

basics

~20 s

You can create and fill an array in one step with braces, like int[] a = {1, 2, 3};. The length comes from the number of values. The bare {1,2,3} form only works at declaration; elsewhere you must write new int[]{1,2,3}.

open as a page