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
DSA Tutorial - Learn Data Structures and Algorithms
DSA (Data Structures and Algorithms) is the study of organizing data efficiently using data structures like arrays, stacks, and trees, paired with step-by-step procedures (or algorithms) to solve problems effectively. Data structures manage how data is stored and accessed, while algorithms focus on
7 min read
Java Tutorial
Java is a high-level, object-oriented programming language used to build web apps, mobile applications, and enterprise software systems. It is known for its Write Once, Run Anywhere capability, which means code written in Java can run on any device that supports the Java Virtual Machine (JVM).Java s
10 min read
Python Interview Questions and Answers
Python is the most used language in top companies such as Intel, IBM, NASA, Pixar, Netflix, Facebook, JP Morgan Chase, Spotify and many more because of its simplicity and powerful libraries. To crack their Online Assessment and Interview Rounds as a Python developer, we need to master important Pyth
15+ min read
SQL Interview Questions
Are you preparing for a SQL interview? SQL is a standard database language used for accessing and manipulating data in databases. It stands for Structured Query Language and was developed by IBM in the 1970s, SQL allows us to create, read, update, and delete data with simple yet effective commands.
15+ min read
Java OOP(Object Oriented Programming) Concepts
Java Object-Oriented Programming (OOPs) is a fundamental concept in Java that every developer must understand. It allows developers to structure code using classes and objects, making it more modular, reusable, and scalable.The core idea of OOPs is to bind data and the functions that operate on it,
13 min read
Java Interview Questions and Answers
Java is one of the most popular programming languages in the world, known for its versatility, portability, and wide range of applications. Java is the most used language in top companies such as Uber, Airbnb, Google, Netflix, Instagram, Spotify, Amazon, and many more because of its features and per
15+ min read
Non-linear Components
In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
Quick Sort
QuickSort is a sorting algorithm based on the Divide and Conquer that picks an element as a pivot and partitions the given array around the picked pivot by placing the pivot in its correct position in the sorted array. It works on the principle of divide and conquer, breaking down the problem into s
12 min read
Merge Sort - Data Structure and Algorithms Tutorials
Merge sort is a popular sorting algorithm known for its efficiency and stability. It follows the divide-and-conquer approach. It works by recursively dividing the input array into two halves, recursively sorting the two halves and finally merging them back together to obtain the sorted array. Merge
14 min read
Breadth First Search or BFS for a Graph
Given a undirected graph represented by an adjacency list adj, where each adj[i] represents the list of vertices connected to vertex i. Perform a Breadth First Search (BFS) traversal starting from vertex 0, visiting vertices from left to right according to the adjacency list, and return a list conta
15+ min read