How to Create an Array in Java?Last Updated : 16 Jan 2026 In Java, an array is an object. It is a collection or group of similar data types. Elements of an array are stored in a contiguous memory location. An array in Java is index-based; the first element of the array is stored at the 0th index, 2nd element is stored at the 1st index and so on. Creating an Array in JavaWe can create an array by following the steps given below: Declaration: First, specify the data type of the elements you want to store in an array, followed by a pair of square brackets and the array's name. Allocation: As we know, an array is an object in Java. So, we will use the new keyword to allocate memory for the array. It specifies the size of the array (number of elements that an array will hold). The size of the array is specified within square brackets. Initialization (Optional): Initialization of an array at the time of declaration is optional. We can initialize it later.
Combined Declaration, Allocation, and Initialization We can declare, allocate, and initialize an array in a single statement. 1. Array Declaration and AllocationOne of the successions used to declare the array is by using the keyword new. It instructs Java to allocate memory for the declared array. ExampleCompile and RunOutput: Element at index 0: 10 Element at index 1: 20 Element at index 2: 30 Element at index 3: 40 Element at index 4: 50 2. Array InitializationWe can also initialize an array directly with values when declaring it, bypassing the need for assigning values individually. ExampleCompile and RunOutput: Fruit at index 0: Apple Fruit at index 1: Banana Fruit at index 2: Orange Fruit at index 3: Grapes 3. Creating Multidimensional (Two-Dimensional) ArraysMulti-dimensional arrays in Java are basically arrays of arrays. It allows us to store data in a tabular or grid-like structure, making it useful for scenarios like matrices, game boards, or any data that requires multiple dimensions. It is useful when we want to store data in tabular form (rows and columns). To read more Multidimensional Arrays in Java Declaration or or We can declare and initialize a two-dimensional array in a single statement as follows. Assigning Values Examples Example of a Two-Dimensional ArrayExampleCompile and RunOutput: 1 2 3 4 5 6 7 8 9 4. Using Arrays.fill() Method for InitializationJava provides a utility class named Arrays that contains a number of methods for array operations. The Arrays.fill() method provides the facility to set an initial value for an array equal to its size. ExampleCompile and RunOutput: Element at index 0: 42 Element at index 1: 42 Element at index 2: 42 Element at index 3: 42 Element at index 4: 42 5. Creating Jagged Arrays (Arrays of Arrays)A jagged array is a collection of arrays where each row may contain a variable number of elements. Jagged arrays are also known as "ragged arrays" or "irregular arrays". They can be created by specifying the size of each array in the declaration. For example, a jagged array with three rows can have the first row with three elements, the second with two elements, and the third with four elements. To read more Jagged Array in Java ExampleCompile and RunOutput: 1 2 3 4 5 6 7 8 9 Next TopicC vs C++ vs Java |
We request you to subscribe our newsletter for upcoming updates.

We deliver comprehensive tutorials, interview question-answers, MCQs, study materials on leading programming languages and web technologies like Data Science, MEAN/MERN full stack development, Python, Java, C++, C, HTML, React, Angular, PHP and much more to support your learning and career growth.
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India