What is the difference between int[] a and int a[] in Java? There is no difference in these two types of array declaration. There is no such difference in between these two types of array declaration. It's just what you prefer to use, both are integer type arrays.
Internally, Array<Int> creates an array of type Integer. It is just like creating an instance of the Integer class in Java. Values created using this syntax will be boxed. Whereas, IntArray creates an array similar to a Java primitive array.
Since int[] is a class, it can be used to declare variables. For example, int[] list; creates a variable named list of type int[]. This variable is capable of referring to an array of ints, but initially its value is null (if it is a member variable in a class) or undefined (if it is a local variable in a method).
A Java both int and Integer are used to store integer type data the major difference between both is type of int is primitive while Integer is of class type.
They are semantically identical. The int array[]
syntax was only added to help C programmers get used to java.
int[] array
is much preferable, and less confusing.
There is one slight difference, if you happen to declare more than one variable in the same declaration:
int[] a, b; // Both a and b are arrays of type int
int c[], d; // WARNING: c is an array, but d is just a regular int
Note that this is bad coding style, although the compiler will almost certainly catch your error the moment you try to use d
.
There is no difference.
I prefer the type[] name
format at is is clear that the variable is an array (less looking around to find out what it is).
EDIT:
Oh wait there is a difference (I forgot because I never declare more than one variable at a time):
int[] foo, bar; // both are arrays
int foo[], bar; // foo is an array, bar is an int.
No, these are the same. However
byte[] rowvector, colvector, matrix[];
is equivalent to:
byte rowvector[], colvector[], matrix[][];
Taken from Java Specification. That means that
int a[],b;
int[] a,b;
are different. I would not recommend either of these multiple declarations. Easiest to read would (probably) be:
int[] a;
int[] b;
From section 10.2 of the Java Language Specification:
The [] may appear as part of the type at the beginning of the declaration, or as part of the declarator for a particular variable, or both, as in this example:
byte[] rowvector, colvector, matrix[];
This declaration is equivalent to:
byte rowvector[], colvector[], matrix[][];
Personally almost all the Java code I've ever seen uses the first form, which makes more sense by keeping all the type information about the variable in one place. I wish the second form were disallowed, to be honest... but such is life...
Fortunately I don't think I've ever seen this (valid) code:
String[] rectangular[] = new String[10][10];
The two commands are the same thing.
You can use the syntax to declare multiple objects:
int[] arrayOne, arrayTwo; //both arrays
int arrayOne[], intOne; //one array one int
see: http://java.sun.com/docs/books/jls/second_edition/html/arrays.doc.html
No difference.
Quoting from Sun:
The
[]
may appear as part of the type at the beginning of the declaration, or as part of the declarator for a particular variable, or both, as in this example:byte[] rowvector, colvector, matrix[];
This declaration is equivalent to:
byte rowvector[], colvector[], matrix[][];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With