Before Java can store values inside an array, the program needs to declare the array variable. Array declaration tells Java what type of elements the array will hold and identifies the variable that will later refer to the array.
Basic Array Declaration
The most common Java syntax for declaring an array places square brackets after the data type.
int[] numbers;
Here, int specifies that the array will store integer values, while [] indicates that the variable represents an array. The variable numbers has been declared, but no actual array has been created yet.
Remember: Declaring an array variable and creating an array are two different operations. Declaration tells Java the variable's intended type; creation allocates the actual array object.
Declaring Arrays of Different Types
An array can be declared for any valid Java data type, including primitive types and reference types.
int[] ages; double[] prices; char[] letters; boolean[] results; String[] names;
Each declaration describes a different kind of array. For example, prices can hold decimal values, while names can hold references to String objects.
| Declaration | Array Element Type | Example Use |
|---|---|---|
| int[] ages | int | Student ages |
| double[] prices | double | Product prices |
| char[] letters | char | Characters |
| boolean[] results | boolean | True/false status |
| String[] names | String | Names |
Creating an Array After Declaration
A declared array variable does not automatically contain an array. To create the array, use the new keyword followed by the element type and the required size.
int[] numbers; numbers = new int[5];
The first statement declares the variable. The second statement creates an array capable of holding five integers and assigns that array to numbers.
Important: The number inside square brackets represents the array length, not the last index. For new int[5], the valid indexes are 0 through 4.
Declaration and Creation in One Statement
In real-world Java code, declaration and creation are often combined into a single statement.
int[] numbers = new int[5];
This statement declares the variable, creates an integer array with five positions, and connects the variable to that array.
Understanding the Three Steps
It is useful to mentally separate array work into three stages: declaration, creation, and initialization.
int[] numbers; // Declaration numbers = new int[5]; // Creation numbers[0] = 10; // Initialization
Declaration introduces the array variable. Creation allocates the array with a fixed number of positions. Initialization places actual values into those positions.
Default Values After Array Creation
When an array is created using new, Java automatically assigns default values to its elements. This is an important difference from local variables, which must be initialized before they are read.
int[] numbers = new int[3]; System.out.println(numbers[0]); System.out.println(numbers[1]); System.out.println(numbers[2]);
All three elements initially contain 0 because the array stores integers.
| Array Type | Default Value |
|---|---|
| int | 0 |
| double | 0.0 |
| char | '\u0000' |
| boolean | false |
| Reference types | null |
Alternative Declaration Syntax
Java also allows the square brackets to appear after the variable name.
int numbers[];
This is valid Java syntax and means the same thing as int[] numbers;. However, placing the brackets next to the data type is generally preferred because it makes the array type easier to recognize.
Best Practice: Prefer int[] numbers over int numbers[]. The first style clearly communicates that the type is an integer array.
Declaring Multiple Arrays
Java allows multiple variables to be declared in one statement, but there is an important syntax detail to understand.
int[] first, second, third;
All three variables above are integer-array variables. They can later refer to separate arrays.
int[] first, second; first = new int[3]; second = new int[5];
Here, first refers to an array of length 3, while second refers to an array of length 5. Each variable can point to a different array object.
A Common Declaration Mistake
When declaring multiple variables using the alternative bracket style, beginners sometimes misunderstand which variables are arrays.
int a[], b;
In this declaration, a is an integer array, but b is a normal integer variable. This is one reason the preferred style is clearer.
int[] a, b;
Now both a and b are declared as integer-array variables.
Arrays as Reference Variables
An array in Java is an object. The array variable itself holds a reference to that object rather than containing the complete collection directly.
int[] numbers = new int[5];
The expression new int[5] creates an array object. The variable numbers refers to that object.
This concept becomes especially important when you later study array copying, method parameters, object references, and memory behavior in Java.
Common Beginner Mistakes
- Declaring an array and assuming it has already been created.
- Using the array length as the last valid index.
- Trying to change an array's length after creation.
- Confusing int[] a, b with int a[], b.
- Expecting newly created integer arrays to contain random values instead of Java's default value of 0.
Interview Insight
An interviewer may ask, “What is the difference between array declaration and array creation?” A precise answer is: declaration defines an array variable and its type, while creation uses new to allocate an actual array object with a specified length.
| Concept | Example | Purpose |
|---|---|---|
| Declaration | int[] numbers; | Declares an array variable |
| Creation | new int[5] | Creates an array object |
| Assignment | numbers = new int[5]; | Connects the variable with the array |
| Initialization | numbers[0] = 10; | Stores a value in an element |
Array declaration is simple, but understanding what happens between declaration, creation, and initialization gives you a much stronger foundation. Once this distinction is clear, array initialization becomes the natural next step.
