Saturday, November 16, 2024
Google search engine
HomeData Modelling & AIIs there any difference between int a and int a in Java?

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

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.

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments