Saturday, June 4, 2022
HomeWordPress DevelopmentIs there any distinction between int a and int a in Java?

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


Enhance Article

Save Article

Like Article

An array in Java is a bunch of like-typed variables referred to by a typical identify. Arrays in Java work in a different way than they do in C/C++.  In Java, Array could be declared within the following methods:

One-Dimensional Arrays: The overall type of a one-dimensional array declaration is 

kind var-name[];
OR
kind[] 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

Distinction between “int[] a” and “int a[]” for 1-D Arrays in Java

For 1-D Array, in Java, there isn’t any distinction and any of the talked about syntaxes can be utilized to declare a 1-D array.

For instance:

Java

  

class GFG {

    public static void essential(String[] args)

    {

        

        

        int[] arr1;

        arr1 = new int[5];

        arr1[0] = 10;

        arr1[1] = 20;

        arr1[2] = 30;

        arr1[3] = 40;

        arr1[4] = 50;

  

        

        

        for (int i = 0; i < arr1.size; i++)

            System.out.println("Array from methodology 1: "

                               + arr1[i]);

        System.out.println();

  

        

        

        int arr2[];

        arr2 = new int[5];

        arr2[0] = 1;

        arr2[1] = 2;

        arr2[2] = 3;

        arr2[3] = 4;

        arr2[4] = 5;

  

        

        

        for (int i = 0; i < arr2.size; i++)

            System.out.println("Array from methodology 2: "

                               + arr2[i]);

    }

}

Output

Array from methodology 1: 10
Array from methodology 1: 20
Array from methodology 1: 30
Array from methodology 1: 40
Array from methodology 1: 50

Array from methodology 2: 1
Array from methodology 2: 2
Array from methodology 2: 3
Array from methodology 2: 4
Array from methodology 2: 5

Distinction between “int[] a” and “int a[]” for a number of Array declarations in Java

Whereas declaring a number of Arrays in Java on the identical time, the tactic of declaration is necessary and must comply with the right syntax. If not, it’s going to end in compile-time errors.

  • Appropriate syntax to declare a number of arrays
    int []a, b;

    For instance:

    Java

      

    import java.io.*;

      

    class GFG {

        public static void essential(String[] args)

        {

            int[] a, b;

            

            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 a number of arrays
    int a[], b;
    int a, b[];

    Instance 1: Instance to point out output for int a[], b declaration.

    Whereas utilizing int a[], b methodology to declare a number of arrays in Java, the compiler will declare “a” as an array, whereas “b” will likely be declared as an integer variable. Therefore whereas accessing this may give a compiler error.

    Java

      

    import java.io.*;

      

    class GFG {

        public static void essential(String[] args)

        {

      

            

            

            

            

            

            

            

            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 varieties: int[] can't be transformed to int
            b = new int[4];
                ^
    prog.java:30: error: array required, however int discovered
                b[i] = i;
                 ^
    prog.java:31: error: array required, however int discovered
                System.out.print(b[i] + " ");
                                  ^
    3 errors

    Instance 2: Instance to point out output for int a, b[] declaration.

    Whereas utilizing int a, b[] methodology to declare a number of arrays in Java, the compiler will declare “a” as an integer variable, whereas “b” will likely be declared as an integer array. Therefore whereas accessing this may give a compiler error.

    Java

      

    import java.io.*;

      

    class GFG {

        public static void essential(String[] args)

        {

      

            

            

            

            

            

            

            

            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 varieties: int[] can't be transformed to int
            b = new int[4];
                ^
    prog.java:30: error: array required, however int discovered
                b[i] = i;
                 ^
    prog.java:31: error: array required, however int discovered
                System.out.print(b[i] + " ");
                                  ^
    3 errors

Distinction between “int[] a” and “int a[]” for Multidimensional Arrays in Java

For Multidimensional Arrays, in Java, there isn’t any distinction and any of the talked about syntaxes can be utilized for declaration.

For instance:

Java

public class multiDimensional {

    public static void essential(String args[])

    {

        

        

        int[][] arr1

            = { { 2, 7, 9 }, { 3, 6, 1 }, { 7, 4, 2 } };

  

        

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

  

        

        

        int arr2[][] = { { 10, 20, 30 }, { 40, 50, 60 } };

  

        

        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 extra most popular syntax amongst “int[] a” and “int a[]” to declare an array?

  • Although, there isn’t any distinction in performance between each forms of declaration. Each declare an array of integers, thus, there isn’t any conclusion which model is extra preferable, int[] a is the popular syntax to declare an array in Java whereas int a[] was included to assist the standard C/C++ programmers.
  • Technically, each syntaxes are the identical within the case of declaring a single array. However each declarations give totally different outcomes if we declare a number of arrays in a single assertion.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments