Array Java: Declare, Define, and Access Array

Java arrays are used for storing elements of the same type together in a contiguous manner. Let us learn how to use arrays Java through this article.
authorImageVarun Saharawat30 Oct, 2025
Array Java: Declare, Define, and Access Array

Array Java: Array is a simple and crucial data structure of Java with many Java programming applications. Array in Java is used to store elements of the same type to be stored together in a contiguous manner. The elements in the array can be easily accessed with their index numbers. 

Regardless of your level of experience as a Java programmer, knowledge of array Java is important. Let us dive into declaring, defining, and implementing arrays in Java.

What are Arrays in Java?

Arrays in Java are a fundamental part of data structure and programming used to store similar elements in a contiguous manner. The elements in the array are stored at a particular index, starting from 0, 1, 2,....., and so on. Elements at a particular location can be accessed using their index number. It is used in many applications in Java programming

Array Java: Important Highlights

In Java, an array allows storing multiple values of the same data type with a single variable name. Let us get more familiar with the array in Java by learning some important facts about the array.
  • Arrays have fixed size and must be declared at the time of declaration. 
  • Each element in the array needs to belong to the same data type. For example, if you declare an array Java of integer data type, you must only put integer elements. 
  • Arrays in Java start their indexing from zero. The first element in the array Java, can be accessed on index zero.
  • The elements in the arrays are stored at contiguous memory locations.
  • Elements stored inside the array can be accessed directly by their index number.

Recommended Technical Course

Array Java: Declaring Array in Java 

The simplest way to declare an array in Java is by using the given syntax.
Array Java
Data type [ ] name_of_Arrray OR Data type name_of_Array[ ];
In the table, data type represents various data types such as string, integer, double, etc. Also, name_of_Array stands for the name of the array. Some common examples of declaring arrays with different data types are mentioned below.
Array Java
int [ ] arr = new int[ ]; string names [ ] =  new String [ ] {} boolean [ ] flags = new boolean [ ];

Array Java: How to declare Array in Java with default values

In Java, we can easily declare an array using default values. Consider a case when you know the size of your array but are not sure which elements to put in then, you can declare an array using default values. Just put the size of elements that the array will contain.
Array Java
Data type [ ] name_of_Array =  new Data type [length of array]

Array Java: Declaration and Implementation of 2D Arrays 

Multidimensional or 2D arrays can be implemented using two square brackets in Java.
Array Java
Data type [ ][ ] name_of_Array =  new Data type [length][length]
The first bracket represents row in the array and the second bracket represents column. This array can be used to represent matrices. Check an example below for implementing 2D array in Java.
Array Java: Implementation of 2D Array in Java
public class TwoDArrayExample {     public static void main(String[] args) { // Declaration and initialization of a 2D array with 3 rows and 3 columns         int[][] arr = {             {1, 2, 3},             {4, 5, 6},             {7, 8, 9}         };         // Accessing and printing elements in the 2D array         System.out.println("Printing the 2D array:");         for (int i = 0; i < arr.length; i++) {             for (int j = 0; j < arr[i].length; j++) {                 System.out.print(arr[i][j] + "\t");             }             System.out.println(); // Move to the next line for the next row         }         // Accessing a specific element in the array         int elementAtRow2Column3 = arr[1][2];         System.out.println("\nElement at row 2, column 3: " + elementAtRow2Column3);         // Modifying an element in the array         arr[1][2] = 99;         System.out.println("\n2D array after modifying an element:");         // Printing the modified array         for (int i = 0; i < arr.length; i++) {             for (int j = 0; j < arr[i].length; j++) {                 System.out.print(arr[i][j] + "\t");             }             System.out.println();         }     } }

Array Java: Types of Arrays

There are two major classifications of arrays in Java. See more information about them below.

1. One-dimensional array

In a single-dimension array, elements are stored in one row. They store elements in a linear sequence.
Array Java: Single Dimension Array
int[] numbers = {1, 2, 3, 4, 5}; // Accessing elements by index int thirdElement = numbers[2]; 
The output for the above ‘thirdElement’ will be three in the above code of a single array.

2. Multi-Dimensional Array

This array is a combination of two or more elements in multiple dimensions. In a two-dimensional array, elements are stored in rows and columns in a 2D structure. The initialization of these arrays is as follows. 

int [][] array = new int [2][4]

array[0][0] array[0][1] array[0][2] array[0][3]
array[1][0] array[1][1] array[1][2] array[1][3]
The above array is based on zero-based indexing. There are two rows and four columns in the array.
Array Java: Multi Dimension Array
int[][] matrix = {             {1, 2, 3, 4},             {5, 6, 7, 8},             {9, 10, 11, 12}         };  // Accessing and printing elements in the 2D array         for (int i = 0; i < matrix.length; i++) {             for (int j = 0; j < matrix[i].length; j++) {                 System.out.print(matrix[i][j] + "\t");             }             System.out.println(); // Move to the next line for the next row         }     } }

Array Java: Benefits of using Java Array

The array is the simplest data structure in Java used in programming. Some of the best benefits of using an array in Java are mentioned below.
  • Arrays allocate memory in a contiguous location that can be accessed quickly. The index number of an array can be used to access any element within it.
  • Elements in the array can be accessed randomly using their index number.
  • The use of an array in Java is simple and efficient when you have a large collection of similar elements.
  • Many operations, such as searching, deleting and other calculations, are easier in an array.
  • Arrays are used for various applications in Java programming, ranging from complex algorithms to data structures. They help implement data structures such as lists, matrices, stacks, etc.
  • In Java, there are many utility functions and methods that can be used with java.util.arrays. It makes sorting, searching, comparing, and other calculations easier in array Java.

Also Read: Armstrong Number In Java: Everthing You Need to Know

Array Java: Disadvantages of using Array 

Arrays are simple and efficient data structure which is easy to use and is preferred by many programmers. However, there are some demerits of using an array due to which it is not preferred everywhere.
  • Arrays have a fixed size which creates a problem when we need to change the size during runtime after declaring.
  • Arrays are well suited for simple tasks with a limited size.
  • Only the same type of data elements can be put inside an array.
  • It needs a large amount of data waste. Suppose we only filled three spaces in an array of size five. The memory is reserved for five but used only for three, leading to memory wastage.
  • In the case of large records, it may take more space than required.
  • Insertion and deletion are ineffective in arrays, as we need to move each element to reach its original location.
  • Also, you must know the exact index of the element to access it directly in an array.

Java Array: Addition using Java Arrays

Check the code below for addition using arrays in Java.
Array Java: Addition using Java Array
public class ArrayAddition {     public static void main(String[] args) {         // Let us take two example arrays below array1 and array 2         int[] array1 = {1, 2, 3, 4, 5};         int[] array2 = {6, 7, 8, 9, 10};         // Check if both arrays are of the same length         if (array1.length != array2.length) {             System.out.println("Arrays must be of the same length for addition.");             return;         }         // Perform addition and store the result in a new array         int[] resultArray = new int[array1.length];         for (int i = 0; i < array1.length; i++) {             resultArray[i] = array1[i] + array2[i];         }         // Display the result         System.out.print("Resultant array after addition: ");         for (int num : resultArray) {             System.out.print(num + " ");         }     } }

Output

Resultant Array after addition 55

Learn Java Programming with DSA

Join our Decode Java Programming with DSA Course to prepare for job roles such as Full Stack Web developer, Frontend engineer and backend engineer. Design and implement responsive and dynamic websites by learning important technologies such as HTML, CSS, Javascript, React, ExpressJS, NodeJS, PHP, and much more.  Also, get hundred percent placement assistance, course completion certificate, doubt solving sessions, industry-relevant projects and much more. Know more only at @pwskills.com.

For Latest Tech Related Information, Join Our Official Free Telegram Group : PW Skills Telegram Group

Array Java FAQs

What are arrays in Java?

Arrays in Java are a fundamental part of data structure and programming used to store similar elements in a contiguous manner. Regardless of your level of programming experience, you have undoubtedly encountered this data structure while learning programming.

Can we access elements randomly in an array?

We can use index to go to a particular element and access its values. However, knowing the exact index is important to yield correct output.

What is the syntax of arrays in Java?

The syntax of an array in Java is data_type [ ] variable_name = new int [ ]. Check out the article to know how to declare a multidimensional array in Java.

What is a declaration in an array?

Declaration of an array is to specify the name and type of element to be kept inside the array. It also specifies the number of elements in the array.