Is there any difference between int[] a and int a[] in Java?


Array is a linear data structure that is used to store a group of elements with similar datatypes. It stores data in a sequential manner. Once we create an array we can't change its size i.e. it is of fixed length. There are various ways to create an array named 'a[]' of type integer including 'int[] a' and 'int a[]'. But, the question that pops up here is that is there any difference between these two syntaxes and which one is preferable. Stick with us till the end of this article to find out the answer to these questions.

How int[ ] a is different from int a[ ] in Java?

Let's first look at the various ways to declare arrays −

Syntax to declare an array

Data_Type[] nameOfarray; // declaration
Or,
Data_Type nameOfarray[]; // declaration
Or,
   // declaration with size
Data_Type nameOfarray[] = new Data_Type[sizeofarray]; 
Or,
   // declaration and initialization
Data_Type nameOfarray[] = {values separated with comma};

To declare an array, we can use any of the above syntaxes. However, choosing one of these options depends on the needs and convenience. For instance, suppose we know how many elements are going to store in the array, then we can use the third way and if we know the exact elements, then we can use the last one.

int[ ] a VS int a[ ]

If one asks what is the difference between 'int[] a' and 'int a[]'. The short answer to this question is that there is no difference between both declarations. Both are valid and equivalent in Java. However, there are a few points we must keep in mind while choosing one over another.

The first point is readability. If we declare only a single integer array then declaring the array as 'int a[]' is fine. But, suppose we have to work with multiple arrays then declaring 'int[] a, b, c' is preferable rather than 'int a[], b[], c[]'.

The second point is consistency. It is acceptable to declare an array as 'int a[]' but it is not valid for methods that return or accept arrays as parameters, such as public int[] getArray() or public void setArray(int[] array) are valid.

Hence, it makes more sense to declare 'int[] a' rather than 'int a[]'. Now, let's see these points practically with examples.

Example 1

In the following example, we will use 'int[] a' to define the first array and 'int a[]' to define the second array. This code will work fine and print the desired result as both ways are valid for 1-D array.

public class Example1 {
   public static void main(String []args) {
      // creating array using 'int[] a'
      int[] aray1  = { 15, 25, 35, 45, 55}; 
      System.out.println("Elements of Array1: ");
      // printing the elements of array 1
      for (int i = 0; i < aray1.length; i++) {
         System.out.println("Index " + i + ": " + aray1[i]);
      }
      // creating array using 'int a[]'
      int aray2[]  = { 11, 22, 33, 44, 55}; 
      System.out.println("Elements of Array2: ");
      // printing the elements of array 2
      for (int i = 0; i < aray2.length; i++) {
         System.out.println("Index " + i + ": " + aray2[i]);
      }
   }
}

Output

Elements of Array1: 
Index 0: 15
Index 1: 25
Index 2: 35
Index 3: 45
Index 4: 55
Elements of Array2: 
Index 0: 11
Index 1: 22
Index 2: 33
Index 3: 44
Index 4: 55

Example 2

In the following example, we will declare two arrays in a single line using 'int[] a, b' approach. This code will also work fine and print the desired result as it is the preferred way of declaring multiple arrays.

public class Example2 {
   public static void main(String []args) {
      // declaring two arrays
      int[] aray1, aray2; 
      aray1 = new int[3];
      aray2 = new int[3];
      // initializing the array1
      aray1[0] = 15;
      aray1[1] = 25;
      aray1[2] = 35;
      // initializing the array2
      aray2[0] = 11;
      aray2[1] = 22;
      aray2[2] = 33;
      // printing the elements of array 1
      System.out.println("Elements of Array1: ");
      for (int i = 0; i < aray1.length; i++) {
         System.out.println("Index " + i + ": " + aray1[i]);
      }
      // printing the elements of array 2
      System.out.println("Elements of Array2: ");
      for (int i = 0; i < aray2.length; i++) {
         System.out.println("Index " + i + ": " + aray2[i]);
      }
   }
}

Output

Elements of Array1: 
Index 0: 15
Index 1: 25
Index 2: 35
Elements of Array2: 
Index 0: 11
Index 1: 22
Index 2: 33

Conclusion

In this article, we have learned about arrays and various ways of declaring an array. Also, we discussed which is more convenient and should be preferred between 'int[] a' and 'int a[]'. Although there is no difference between these syntaxes, 'int[] a' provides more consistency and readability to our code.

Updated on: 17-Aug-2023

179 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements