Arrays in Java

Arrays are a fundamental data structure in programming, and Java is no exception. In this post, we’ll take a deep dive into the world of arrays in Java, including:

  • How to create or declare an array in Java
  • How to initialize an array in Java
  • How to access an array element in Java
  • How to manipulate array elements in Java and
  • Some common pitfalls to watch out for

Creating or Declaring an Array in Java

Array declaration or creation in Java is relatively straightforward. The syntax is as follows:

type[] arrayName = new type[size];

Here, type is the data type of the elements in the array (e.g. int, double, String, etc.), arrayName is the name of the array variable, and size is the number of elements the array can hold. For example, the following code creates an array of integers with the name myArray and a size of 10:

int[] myArray = new int[10];

This means that the variable myArray can hold a maximum 10 number of elements and the first index of the array always starts with 0. Hence, the maximum index of the above array will be 9.

Initializing an Array in Java

It’s also possible to create an array and initialize its elements at the same time. The syntax for this is as follows:

type[] arrayName = {element1, element2, element3, ...};

For example, the following code creates an array of strings with the name myArray and initializes it with the elements “apple”, “banana”, “cherry”:

String[] myArray = {"apple", "banana", "cherry"};

Accessing and Manipulating Elements

Once an array is created, we can access and manipulate its elements using the array name followed by the index of the element in square brackets. For example, the following code sets the first element of myArray (with index 0) to the value 42:

myArray[0] = 42;

We can also use the elements of an array in expressions and assignments. For example, the following code multiplies the second element of myArray (with index 1) by 2 and assigns the result to a variable named result:

int result = myArray[1] * 2;

It’s worth noting that array indices in Java are zero-based, meaning the first element has index 0, the second element has index 1, and so on. Attempting to access an element with an index that is less than 0 or greater than or equal to the size of the array will result in an ArrayIndexOutOfBoundsException.

Common pitfalls to remember in Arrays in Java

  • Always use Arrays.equals method to compare elements in arrays instead of == operator.

Don’t use the == operator to compare elements in an array

One common pitfall when working with arrays in Java is attempting to use the == operator to compare two arrays. The == operator compares the memory addresses of the arrays, not the elements they contain. We can see an example below:

int[] arr1 = {1,2,3,4};
int[] arr2 = {1,2,3,4};

The output of the above code will be false because as mentioned earlier, the == operator compares the memory address of the array, not the elements.

Always use Arrays.equals(arr1, arr2) method to compare elements

To compare the elements of two arrays, we can use the Arrays.equals(array1, array2) method. Let’s see an example below and see the output what it returns.

int[] arr1 = {1,2,3,4};
int[] arr2 = {1,2,3,4};
System.out.println(Arrays.equals(arr1, arr2));

The output of the above code will be true because the Arrays.equals method compares the element available inside arrays.

  • Another pitfall is to assume that the length of an array can be changed after it’s created. Once an array is created, its size is fixed and cannot be changed. If it needs a dynamic data structure that can grow and shrink, we can always use the ArrayList class instead.


Arrays in Java are a powerful and versatile data structure. Mastering their use is essential for any Java developer. If you know how to create, initialize, access, and manipulate elements of an array then it will really help you while working in Java. Hence, it is always good to remember the things mentioned in this blog post.