The Wayback Machine - https://web.archive.org/web/20240621164814/https://www.geeksforgeeks.org/is-there-any-difference-between-int-a-and-int-a-in-java/
Open In App

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

Last Updated : 18 Jan, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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]);
    }
}


Output

Array 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] + " ");
        }
    }
}


Output

array 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();
        }
    }
}


Output

2 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

TimeZone getOffset(int, int, int, int, int, int) Method in Java with Examples
The getOffset(int era, int yr, int mon, int day, int dayOfWeek, int millisec) method of TimeZone class in Java is used to know the offset value of this TimeZone at a specific date or modified date in case of daylight savings, from the UTC or the Universal Time Coordinated. This offset value can be added to get the local time. Syntax: public abstrac
2 min read
ZoneOffset ofHoursMinutesSeconds(int, int, int) method in Java with Examples
The ofHoursMinutesSeconds(int, int, int) method of ZoneOffset Class in java.time package is used to obtain an instance of ZoneOffset using the offset in hours, minutes and seconds passed as the parameter. This method takes the hours, minutes and seconds as parameter in the form of int and converts it into the ZoneOffset. Syntax: public static ZoneO
2 min read
SimpleTimeZone setStartRule(int, int, int) method in Java with Examples
The setStartRule(int startofMonth, int startofDay, int startofTime) method of SimpleTimeZone class in Java is used to set a particular start rule of the day-light saving time to a fixed date within a given month. Syntax: public void setStartRule(int startofMonth, int startofDay, int startofTime) Parameters: The method takes three parameters: starto
2 min read
SimpleTimeZone setEndRule(int, int, int) method in Java with Examples
The setEndRule(int endofMonth, int endofDay, int endofTime) method of SimpleTimeZone class in Java is used to set a particular rule of the day-light saving time to a fixed date within a given month. Syntax: public void setEndRule(int endofMonth, int endofDay, int endofTime) Parameters: The method takes three parameters: endofMonth: This is of Integ
2 min read
HijrahDate of(int, int, int) method in Java with Example
The of() method of java.time.chrono.HijrahDate class is used to generate the date according to Islamic hijri calendar system by using the passed proleptic year, month and date. Syntax: public static HijrahDate of(int Year, int month, int dayOfMonth) Parameter: This method takes the following argument as a parameter: year: which is the integer value
2 min read
IsoChronology date(int, int, int) method in Java with Example
The date() method of java.time.chrono.IsoChronology class is used get the local date according to ISO calendar system for the given year, month and day. Syntax: public LocalDate date(int prolepticYear, int month, int dayOfMonth) Parameter: This method takes the following arguments as parameter. prolepticYear: integer proleptic year for the year fie
2 min read
JapaneseChronology date(int, int, int) method in Java with Example
The date() method of java.time.chrono.JapaneseChronology class is used get the local date according to japanese calendar system for the given year, month and day.Syntax: public JapaneseDate date(int prolepticYear, int month, int dayOfMonth) Parameter: This method takes the following arguments as parameter. prolepticYear: integer proleptic year for
2 min read
JapaneseDate of(int, int, int) method in Java with Example
The of() method of java.time.chrono.JapaneseDate class is used to generate the date according to japanese calendar system by using the passed proleptic year, month and date. Syntax: public static JapaneseDate of(int prolepticYear, int month, int dayOfMonth) Parameter: This method takes the following argument as a parameter: year: which is the integ
2 min read
JapaneseDate of(JapaneseEra,int, int, int) method in Java with Example
The of() method of java.time.chrono.JapaneseDate class is used to generate the date according to Japanese calendar system by using the passed proleptic year, month, date and JapaneseEra. Syntax: public static JapaneseDate of(JapaneseEra era, int yearOfEra, int month, int dayOfMonth) Parameter: This method takes the following argument as a parameter
2 min read
MinguoChronology date(int, int, int) method in Java with Example
The date() method of java.time.chrono.MinguoChronology class is used get the local date according to Minguo calendar system for the given year, month and day.Syntax: public MinguoDate date(int prolepticYear, int month, int dayOfMonth) Parameter: This method takes the following arguments as parameter. prolepticYear: integer proleptic year for the ye
2 min read
Article Tags :
Practice Tags :
three90RightbarBannerImg