How to Declare an Array in Java?
Last Updated :
28 Feb, 2025
In Java programming, arrays are one of the most essential data structures used to store multiple values of the same type in a single variable. Understanding how to declare an array in Java is very important. In this article, we will cover everything about array declaration, including the syntax, different ways to declare arrays, and common mistakes to avoid.
What is an Array Declaration in Java?
Declaring an array in Java means defining an array variable without assigning values. It simply creates a reference to an array but does not allocate memory. The actual memory allocation happens when the array is initialized.
Declaration of Array
Declaration of an Array in Java is very straightforward. First, we need to specify the data type of the element, followed by square brackets [], and then we need to write the name of an array.
Syntax:
dataType [] arrayName; // preferred syntax
- DataType: The type of elements the array will hold (e.g., int, double, String).
- arrayName: The name of the array variable.
Alternative Syntax:
dataType arrayName[]; // Less common
Note: This syntax is valid but not preferred.
Examples of Declaring Arrays
// Declares an array of integers
int[] arr;
// Declares an array of strings
String[] arr;
// Declares an array of doubles
double[] arr;
// Declares an array of integers with alternative syntax
int arr[];
Initialize an Array
Declaring an array only creates a reference. We first need to initialize an array before it can be used. We can initialize an array in two different ways which are listed below
1. Static Initialization
At the time of declaration we can assign value to the array
Syntax:
dataType[] arrayName = {value1, value2, value3};
Example:
int[] arr = {11, 22, 33, 44, 55};
String[] names = {"Geek1", "Geek2", "Geek3"};
Static initialization directly assigns values.
2. Dynamic Initialization
We can allocate memory for the array using the new keyword.
Syntax:
dataType[] arrayName = new dataType[arraySize];
Example:
// Array of size 5
int[] num = new int[5];
// Array of size 3
String[] names = new String[3];
Dynamic initialization allocates memory but leaves values unassigned (default values apply).
Note: The size of an array is fixed once it is set, it cannot be changed
Key Points
- Declaring an array only creates a reference, not the actual array.
- Memory is allocated only when we use the new keyword.
- We cannot specify the size of an array at the time of declaration.
- Java arrays are zero-indexed, means the first element is at index 0.
Similar Reads
How to Return an Array in Java? An array is a data structure that consists of a group of elements of the same data type such that each element of the array can be identified by a single array index or key. The elements of the array are stored in a way that the address of any of the elements can be calculated using the location of
5 min read
How to Add an Element to an Array in Java? In Java, arrays are of fixed size, and we can not change the size of an array dynamically. We have given an array of size n, and our task is to add an element x into the array. In this article, we will discuss the NewDifferent Ways to Add an Element to an ArrayThere are two different approaches we c
3 min read
How to Initialize an Array in Java? An array in Java is a linear data structure that is used to store multiple values of the same data type. In an array, each element has a unique index value, which makes it easy to access individual elements. We first need to declare the size of an array because the size of the array is fixed in Java
5 min read
Character Array in Java In Java, a character array is a data structure used to store a sequence of characters. The characters are stored in contiguous memory locations and can be accessed by their index, similar to an array of integers or any other data type. Declaring a Character Array A character array can be declared in
5 min read
Arrays Class in Java The Arrays class in java.util package is a part of the Java Collection Framework. This class provides static methods to dynamically create and access Java arrays. It consists of only static methods and the methods of an Object class. The methods of this class can be used by the class name itself.The
15 min read
Reflection Array Class in Java The Array class in java.lang.reflect package is a part of the Java Reflection. This class provides static methods to create and access Java arrays dynamically. It is a final class, which means it can't be instantiated or changed. Only the methods of this class can be used by the class name itself.Th
7 min read
How to Find Length or Size of an Array in Java? Finding the length of an array is a very common and basic task in Java programming. Knowing the size of an array is essential so that we can perform certain operations. In this article, we will discuss multiple ways to find the length or size of an array in Java.In this article, we will learn:How to
3 min read
Array to ArrayList Conversion in Java In Java, arrays are fixed-sized, whereas ArrayLists are part of the Java collection Framework and are dynamic in nature. Converting an array to an ArrayList is a very common task and there are several ways to achieve it.Methods to Convert Array to an ArrayList1. Using add() Method to Manually add th
3 min read
Array Declarations in Java (Single and Multidimensional) In Java, an Array is used to store multiple values of the same type in a single variable. There are two types of arrays in Java:Single-dimensional arraysMulti-dimensional arraysIn this article, we are going to discuss how to declare and use single and multidimensional arrays in Java.Single-Dimension
6 min read
Passing an Array to a Function in Java Passing an array to a function is an easy-to-understand task in Java. In this article, we will check how to pass an array as a method parameter.Caller Function Vs Called FunctionLet function GFG() be called from another function GFGNews(). Here, GFGNews is called the âCaller Functionâ and GFG is cal
2 min read