What is the array initializer shorthand in Java, and when can you use the curly-brace form?
answer
- Bare {1,2,3} only in a declaration
- new int[]{...} for any other position
- Never size + initializer together
- Length inferred from element count
- Nested braces for multi-dim; trailing comma ok
basics
~20 sYou 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}.
solid answer
~40 sJava has two initializer styles. The **declaration shorthand** `int[] a = {1, 2, 3};` declares, allocates, and fills in one statement — the length is inferred from the element count. This bare-brace form is only valid in a variable declaration. Anywhere else (assignment, method argument, return) you must use the **anonymous array expression** `new int[]{1, 2, 3}`, which is the same thing but as an expression you can place anywhere. Crucially, you cannot give both a size and an initializer: `new int[3]{...}` is a compile error — the length must come from one source or the other. Initializers nest for multi-dimensional arrays (`{{1,2},{3,4}}`), support a trailing comma, and may contain arbitrary expressions, not just constants. The shorthand is the idiomatic way to build small fixed arrays.
code
java · 5 linesint[] a = {1, 2, 3}; // declaration shorthand (length 3)
a = new int[]{4, 5, 6}; // expression form for reassignment
printAll(new int[]{7, 8, 9}); // expression form as argument
int[][] m = {{1, 2}, {3, 4, 5}}; // nested, jagged allowed
// int[] bad = new int[3]{1,2,3}; // compile error: size + initializergo deeper
Uses int[] a = {1,2,3}; to make a small array and knows length is inferred.
Knows bare braces are declaration-only and switches to new int[]{...} elsewhere; avoids the size+initializer error.
Explains it as expression vs. declaration sugar, handles var, nested/jagged initializers, and non-constant element expressions.
Discusses readability/maintainability trade-offs, when a builder/Map.of-style factory beats a literal, and how the inference rules interact with type inference (var, generics).
## The problem it solves Using `new int[3]` then assigning each index is verbose for a known set of values. Java's **array initializer** lets you list the elements in braces and have the compiler size and fill the array for you. ## Form 1: the declaration shorthand ```java int[] a = {1, 2, 3}; ``` This single statement (1) declares `a`, (2) allocates an `int[3]`, and (3) fills it with `1,2,3`. The **length is inferred** from the number of items — you never write a number. This bare-brace `{...}` form is special: it is **only legal directly in a variable declaration**. It is syntactic sugar, not a general expression. ## Form 2: the anonymous (expression) array When you need an array value *somewhere other than a declaration* — as a method argument, an assignment to an existing variable, or a return value — the bare braces won't compile. You use: ```java new int[]{1, 2, 3} ``` This is the same initialized array but written as an **expression**, so it can appear anywhere a value can: ```java a = new int[]{4, 5, 6}; // reassignment printAll(new int[]{7, 8, 9}); // argument return new String[]{"x"}; // return ``` ## The rule you must remember You supply the size **either** with a number **or** with an initializer, never both: - `new int[3]` — size given, default-filled. ✅ - `new int[]{1,2,3}` — initializer given, size inferred. ✅ - `new int[3]{1,2,3}` — **compile error**, size specified twice. ❌ - `int[] a = {1,2,3};` — declaration shorthand. ✅ - `int[] a; a = {1,2,3};` — **compile error**, bare braces outside a declaration. ❌ ## Details that matter - **Trailing comma allowed:** `{1, 2, 3,}` is legal, which makes generated/edited lists easier to diff. - **Empty initializer:** `{}` makes a zero-length array. - **Arbitrary expressions:** elements need not be constants — `{x, y+1, f()}` is fine. - **Type compatibility / inference:** elements must be assignable to the element type; for `new int[]{...}` you state the type, while `int[] a = {...}` takes the type from the declaration. With `var`, you must use the explicit `new` form: `var a = new int[]{1,2,3};` (bare `var a = {1,2,3};` won't compile because there's no type to infer from). ## Multi-dimensional initializers Braces nest: ```java int[][] m = {{1, 2}, {3, 4, 5}}; // jagged is fine: rows can differ ``` ## Bottom line Use `{...}` only at declaration; use `new int[]{...}` everywhere else; never combine an explicit size with an initializer. The length is always inferred from the listed elements.
- Why does new int[3]{1, 2, 3} fail to compile?Because the length is being specified twice — once by the explicit 3 and once implicitly by the three initializer elements. Java requires exactly one source for the length, so you write either new int[3] or new int[]{1,2,3}.
- How would you pass an array literal directly to a method?Use the anonymous form: method(new int[]{1, 2, 3}). The bare-brace shorthand is only allowed in a variable declaration, not as an argument expression.
saying these in an interview costs you the question
- Writing new int[3]{1,2,3} (double-sizing — compile error)
- Using bare braces in an assignment or argument
- Thinking initializer elements must be compile-time constants
- Believing var a = {1,2,3}; compiles (it needs new int[]{...})