Course contentsShow
← Java
Lesson 234 of 2,8706. ArraysFree lesson

Array Declaration Syntax

Learn the two valid syntaxes for declaring arrays in Java: type[] name and type name[].

Array Declaration Syntax

What you'll learn: You'll discover the two valid ways to declare arrays in Java and understand that both are interchangeable.

The Two Syntaxes

Java gives you two options when declaring an array variable. Both are completely valid and produce identical results:

Option 1: Type-first syntax

int[] numbers;
String[] names;
double[] prices;

Option 2: Variable-name syntax

int numbers[];
String names[];
double prices[];

Understanding the Difference

The first syntax (int[] numbers) emphasizes that the type itself is "an array of integers." The brackets are part of the type declaration. This is the preferred modern style because it makes the array nature of the variable immediately clear.

The second syntax (int numbers[]) comes from C and C++, where the brackets follow the variable name. Java supports this for compatibility, but it's less commonly used today.

Think of it this way: Declaring an array is like labeling a storage container. You can say "this is an integer-array container called numbers" (int[] numbers) or "this is a container called numbers that holds integer-arrays" (int numbers[]). Either label works!

Important Note

Both syntaxes only declare the variable—they don't create the actual array yet. At this point, both numbers variables have the value null (they don't point to any array). Creating the array itself comes in a later step.

Multiple Variables in One Declaration

Be careful when declaring multiple variables:

int[] a, b, c;    // All three are int arrays
int d[], e, f;    // Only d is an array; e and f are regular ints!

Key Takeaway: Java allows two array declaration syntaxes—type[] name (preferred) and type name[] (legacy style)—both create array-type variables that can later hold array objects.