Array | Difference Between int[] array and int array[].

In this tutorial, we will learn the Difference Between int[] array and an int array[]. As far as single dimension array is concerned both are the same we can declare in any of the ways but we can not do so for multidimensional arrays. Whereas int array[] is a way to initialize an array in C and C++.

Array

int[] Array

it is a way to initialize an array that is not supported in C or C++ and it is the only one supported in java where square brackets are used before the variable name so let’s take an example to see the use of such initializations.

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

Here we can see the use of square brackets before the array variable name.

int array[]

It is a way to initialize an array in C or C++ and it is also supported in java but only for dimensions arrays when it comes to multi-dimension we know it is not being supported in that case we have to use int[] Array style to initialize the array let us understand with some examples.

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

here one dimension array is initialized with post square brackets.

 

Learn More About Array | Difference Between int[] array and int array[]. at: Difference declaration of array

And also visitJava Tutorials To learn more about java and solve your problems related to java that are faced during solving problems here and also visit  How To Reverse A String In Python? for python related problems and other problems related to programming languages.

Leave a Comment

%d bloggers like this: