What are the ways to declare an array variable in Java, and is there a difference between int[] a and int a[]?
answer
- Brackets on type vs. brackets on name
- int[] a,b → both arrays; int a[],b → only a is array
- Declaration ≠ allocation; reference is null/unassigned
- Type-side form is the Java convention
- Multi-dim: int[][] grid
basics
~20 sYou 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 sJava 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 linesint[] 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 referencego deeper
States both forms exist and that int[] a is the preferred style; knows declaration alone makes no elements.
Explains the multi-variable trap (int a[], b;) and why the type-side form prevents it.
Connects declaration to reference semantics and null state, and applies the convention to parameters/returns and varargs.
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