Is there any difference between int[] a and int a[] in Java?
Last Updated :
18 Jan, 2023
An array in Java is a group of like-typed variables referred to by a common name. Arrays in Java work differently than they do in C/C++. In Java, Array can be declared in the following ways:
One-Dimensional Arrays: The general form of a one-dimensional array declaration is
type var-name[];
OR
type[] var-name;
Multidimensional Arrays:
int[][] intArray = new int[10][20]; //a 2D array or matrix
int[][][] intArray = new int[10][20][10]; //a 3D array
Difference between "int[] a" and "int a[]" for 1-D Arrays in Java
For 1-D Array, in Java, there is no difference and any of the mentioned syntaxes can be used to declare a 1-D array.
For example:
Java
// Java program to illustrate creating an array
// of integers, puts some values in the array,
// and prints each value to standard output.
class GFG {
public static void main(String[] args)
{
// declares an Array of integers
// using method 1
int[] arr1;
arr1 = new int[5];
arr1[0] = 10;
arr1[1] = 20;
arr1[2] = 30;
arr1[3] = 40;
arr1[4] = 50;
// accessing the elements
// of the specified array
for (int i = 0; i < arr1.length; i++)
System.out.println("Array from method 1: "
+ arr1[i]);
System.out.println();
// declares an Array of integers
// using method 2
int arr2[];
arr2 = new int[5];
arr2[0] = 1;
arr2[1] = 2;
arr2[2] = 3;
arr2[3] = 4;
arr2[4] = 5;
// accessing the elements
// of the specified array
for (int i = 0; i < arr2.length; i++)
System.out.println("Array from method 2: "
+ arr2[i]);
}
}
OutputArray from method 1: 10
Array from method 1: 20
Array from method 1: 30
Array from method 1: 40
Array from method 1: 50
Array from method 2: 1
Array from method 2: 2
Array from method 2: 3
Array from method 2: 4
Array from method 2: 5
Time Complexity: O(N), where N is length of array.
Auxiliary Space: O(1)
Difference between "int[] a" and "int a[]" for multiple Array declarations in Java
While declaring multiple Arrays in Java at the same time, the method of declaration is important and needs to follow the proper syntax. If not, it will result in compile-time errors.
- Correct syntax to declare multiple arrays
int []a, b;
For example:
Java
// Program to show array declaration using int[] a, b syntax
import java.io.*;
class GFG {
public static void main(String[] args)
{
int[] a, b;
// Here both a and b are integer arrays
a = new int[3];
b = new int[4];
System.out.print("array a: ");
for (int i = 0; i < 3; i++) {
a[i] = i;
System.out.print(a[i] + " ");
}
System.out.print("\n");
System.out.print("array b: ");
for (int i = 0; i < 4; i++) {
b[i] = i;
System.out.print(b[i] + " ");
}
}
}
Outputarray a: 0 1 2
array b: 0 1 2 3
- Incorrect Declaration of multiple arrays
int a[], b;
int a, b[];
Example 1: Example to show output for int a[], b declaration.
While using int a[], b method to declare multiple arrays in Java, the compiler will declare "a" as an array, whereas "b" will be declared as an integer variable. Hence while accessing this will give a compiler error.
Java
// Program to show array declaration
// using int a[], b syntax
import java.io.*;
class GFG {
public static void main(String[] args)
{
// While using this method to declare
// multiple arrays in Java,
// the compiler will declare a as an array,
// whereas b will be declared
// as an integer variable
// Hence while accessing this
// will give compiler error
int a[], b;
b = new int[4];
a = new int[3];
System.out.print("array a: ");
for (int i = 0; i < 3; i++) {
a[i] = i;
System.out.print(a[i] + " ");
}
System.out.print("\n");
System.out.print("array b: ");
for (int i = 0; i < 4; i++) {
b[i] = i;
System.out.print(b[i] + " ");
}
}
}
Compile Time Error Messages:
prog.java:19: error: incompatible types: int[] cannot be converted to int
b = new int[4];
^
prog.java:30: error: array required, but int found
b[i] = i;
^
prog.java:31: error: array required, but int found
System.out.print(b[i] + " ");
^
3 errors
Example 2: Example to show output for int a, b[] declaration.
While using int a, b[] method to declare multiple arrays in Java, the compiler will declare "a" as an integer variable, whereas "b" will be declared as an integer array. Hence while accessing this will give a compiler error.
Java
// Program to show array declaration
// using int a, b[] syntax
import java.io.*;
class GFG {
public static void main(String[] args)
{
// While using this method to declare
// multiple arrays in Java,
// the compiler will declare a as an array,
// whereas b will be declared
// as an integer variable
// Hence while accessing this
// will give compiler error
int a, b[];
b = new int[4];
a = new int[3];
System.out.print("array a: ");
for (int i = 0; i < 3; i++) {
a[i] = i;
System.out.print(a[i] + " ");
}
System.out.print("\n");
System.out.print("array b: ");
for (int i = 0; i < 4; i++) {
b[i] = i;
System.out.print(b[i] + " ");
}
}
}
Compile Time Error Messages:
prog.java:19: error: incompatible types: int[] cannot be converted to int
b = new int[4];
^
prog.java:30: error: array required, but int found
b[i] = i;
^
prog.java:31: error: array required, but int found
System.out.print(b[i] + " ");
^
3 errors
Difference between "int[] a" and "int a[]" for Multidimensional Arrays in Java
For Multidimensional Arrays, in Java, there is no difference and any of the mentioned syntaxes can be used for declaration.
For example:
Java
public class multiDimensional {
public static void main(String args[])
{
// Syntax 1
// declaring and initializing 2D array
int[][] arr1
= { { 2, 7, 9 }, { 3, 6, 1 }, { 7, 4, 2 } };
// printing 2D array
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++)
System.out.print(arr1[i][j] + " ");
System.out.println();
}
System.out.println();
// Syntax 2
// declaring and initializing 2D array
int arr2[][] = { { 10, 20, 30 }, { 40, 50, 60 } };
// printing 2D array
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++)
System.out.print(arr2[i][j] + " ");
System.out.println();
}
}
}
Output2 7 9
3 6 1
7 4 2
10 20 30
40 50 60
Which is more preferred syntax among "int[] a" and "int a[]" to declare an array?
- Though, there is no difference in functionality between both types of declaration. Both declare an array of integers, thus, there is no conclusion which style is more preferable, int[] a is the preferred syntax to declare an array in Java whereas int a[] was included to help the traditional C/C++ programmers.
- Technically, both syntaxes are the same in the case of declaring a single array. But both declarations give different results if we declare multiple arrays in a single statement.
Similar Reads
Difference between an Integer and int in Java with Examples In Java, int is a primitive data type while Integer is a Wrapper class.int, being a primitive data type has got less flexibility. We can only store the binary value of an integer in it.Since Integer is a wrapper class for int data type, it gives us more flexibility in storing, converting and manipul
6 min read
Difference between Array and String in Java An array is a collection of similar type of elements that are stored in a contiguous memory location. Arrays can contain primitives(int, char, etc) as well as object(non-primitives) references of a class depending upon the definition of the array. In the case of primitive data type, the actual value
5 min read
Difference between List and ArrayList in Java A Collection is a group of individual objects represented as a single unit. Java provides a Collection Framework which defines several classes and interfaces to represent a group of objects as a single unit This framework consists of the List Interface as well as the ArrayList class. In this article
4 min read
What is the difference between lists and arrays? In programming, lists and arrays are data structures used to organize and store data. Both have their unique features and purposes. Lists are dynamic and flexible, allowing for easy resizing during runtime, while arrays are static with a fixed size. This difference impacts memory usage and performan
8 min read
Difference Between byte, short, int and long Datatype in Java There are two types of data types namely primitive datatype/fundamental and non-primitive/derived datatype. The primitive data type is defined as local sets or default set of datatype already present while deriving a datatype or creating a set of datatype using them are known as derived data types s
4 min read