Java array convention: String[] args vs. String args[] - java

I am currently teaching students as a tutor programming conventions. I've told them that they can find most conventions in the Oracle Code Conventions.
In my last tutorial a student asked if:
public static void main(String args[])
or
public static void main(String[] args)
is written by convention or if there is a difference. I have never seen the first version before, so I'm very sure that the second one is a convention. But I don't have a source for that.
Can you give me a source (preferably from oracle, like the page I've linked above) that makes clear which of both is convention?
Equivalence of both expressions
I know that both expressions are equivalent:
The JLS 7, p. 292 states:
An array type is written as the name of an element type followed
by some number of empty pairs of square brackets [].
but also on p. 293:
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.
For example:
byte[] rowvector, colvector, matrix[];
This declaration is equivalent to:
byte rowvector[], colvector[], matrix[][];
But this doesn't help for the convention-quesiton.
So they are identical (not specs, but here is a source).
They produce the same bytecode in a small example, so I'm very sure that they are also identical in praxis.

This is not from Oracle but I think it will help.
It is from Kathy Sierra's book SCJP Sun Certified Programmer for Java 6
int[] key;
int key [];
When declaring an array reference, you should always put the array brackets
immediately after the declared type, rather than after the identifier (variable
name). That way, anyone reading the code can easily tell that, for example, key is a
reference to an int array object, and not an int primitive.

Oracle's Code Conventions do not explicitly state it, but in all examples they use the brackets immediately after the declared type.
In their example code (which should be considered authoritative in this context) they use:
private Object[] instanceVar3;
Also on the page detailing the initialization of variables they have this example that demonstrates the possible problems of putting the brackets behind the variable name:
int foo, fooarray[]; //WRONG!
One could be tempted to do this and think one were declaring several arrays. Althoug this syntactically correct (as brimborium pointed out in the comments), Oracle didn't put the capital letters there for nothing. Better to be safe, clear and also type less by putting the brackets behind the type to show clearly what you want to declare.

There is one obscure use case for the late brackets:
int x, xs[], xxs[][];
How much this is useful, I let the reader be the judge.

One advantage of using [] immediately after array type is:
If you want to declare multiple arrays then you to write like:
int[] a,b,c;
But if you use [] after the array name, then we have to use[] after every array variable like:
int a[],b[],c[];

Related

why does int [][] mat = new int [3][3] and int mat[][] = new int [3][3] doesn't make any difference? [duplicate]

I have recently been thinking about the difference between the two ways of defining an array:
int[] array
int array[]
Is there a difference?
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[][];
There isn't any difference between the two; both declare an array of ints. However, the former is preferred since it keeps the type information all in one place. The latter is only really supported for the benefit of C/C++ programmers moving to Java.
There is no real difference; however,
double[] items = new double[10];
is preferred as it clearly indicates that the type is an array.
It is an alternative form, which was borrowed from C, upon which java is based.
As a curiosity, there are three ways to define a valid main method in java:
public static void main(String[] args)
public static void main(String args[])
public static void main(String... args)
Both are equally valid. The int puzzle[] form is however discouraged, the int[] puzzle is preferred according to the coding conventions. See also the official Java arrays tutorial:
Similarly, you can declare arrays of other types:
byte[] anArrayOfBytes;
short[] anArrayOfShorts;
long[] anArrayOfLongs;
float[] anArrayOfFloats;
double[] anArrayOfDoubles;
boolean[] anArrayOfBooleans;
char[] anArrayOfChars;
String[] anArrayOfStrings;
You can also place the square brackets after the array's name:
float anArrayOfFloats[]; // this form is discouraged
However, convention discourages this form; the brackets identify the array type and should appear with the type designation.
Note the last paragraph.
I recommend reading the official Sun/Oracle tutorials rather than some 3rd party ones. You would otherwise risk end up in learning bad practices.
There is no difference, but Sun recommends putting it next to the type as explained here
The most preferred option is int[] a - because int[] is the type, and a is the name.
(your 2nd option is the same as this, with misplaced space)
Functionally there is no difference between them.
The Java Language Specification says:
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[][];
Thus they will result in exactly the same byte code.
In Java, these are simply different syntactic methods of saying the same thing.
They're the same. One is more readable (to some) than the other.
They are completely equivalent. int [] array is the preferred style. int array[] is just provided as an equivalent, C-compatible style.
Both have the same meaning. However, the existence of these variants also allows this:
int[] a, b[];
which is the same as:
int[] a;
int[][] b;
However, this is horrible coding style and should never be done.
There is no difference in functionality between both styles of declaration. Both declare array of int.
But int[] a keeps type information together and is more verbose so I prefer it.
They are the same, but there is an important difference between these statements:
// 1.
int regular, array[];
// 2.
int[] regular, array;
in 1. regular is just an int, as opposed to 2. where both regular and array are arrays of int's.
The second statement you have is therefore preferred, since it is more clear. The first form is also discouraged according to this tutorial on Oracle.
As already stated, there's no much difference (if you declare only one variable per line).
Note that SonarQube treats your second case as a minor code smell:
Array designators "[]" should be on the type, not the variable (squid:S1197)
Array designators should always be located on the type for better code
readability. Otherwise, developers must look both at the type and the
variable name to know whether or not a variable is an array.
Noncompliant Code Example
int matrix[][]; // Noncompliant
int[] matrix[]; // Noncompliant
Compliant Solution
int[][] matrix; // Compliant
Yep, exactly the same. Personally, I prefer
int[] integers;
because it makes it immediately obvious to anyone reading your code that integers is an array of int's, as opposed to
int integers[];
which doesn't make it all that obvious, particularly if you have multiple declarations in one line. But again, they are equivalent, so it comes down to personal preference.
Check out this page on arrays in Java for more in depth examples.
when declaring a single array reference, there is not much difference between them. so the following two declarations are same.
int a[]; // comfortable to programmers who migrated from C/C++
int[] a; // standard java notation
when declaring multiple array references, we can find difference between them. the following two statements mean same. in fact, it is up to the programmer which one is follow. but the standard java notation is recommended.
int a[],b[],c[]; // three array references
int[] a,b,c; // three array references
Both are ok. I suggest to pick one and stick with it. (I do the second one)
While the int integers[] solution roots in the C language (and can be thus considered the "normal" approach), many people find int[] integers more logical as it disallows to create variables of different types (i.e. an int and an array) in one declaration (as opposed to the C-style declaration).
Yes, there's a difference.
int[] a = new int[100]; // 'a' is not an array itself , the array is stored as an address
elsewhere in memory and 'a' holds only that address
int b[] = new int[100]; // while creating array like cleary shows 'b' is an array and it
is integer type.

What is Boolean [ ] ba[ ] supposed to mean? [duplicate]

I have recently been thinking about the difference between the two ways of defining an array:
int[] array
int array[]
Is there a difference?
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[][];
There isn't any difference between the two; both declare an array of ints. However, the former is preferred since it keeps the type information all in one place. The latter is only really supported for the benefit of C/C++ programmers moving to Java.
There is no real difference; however,
double[] items = new double[10];
is preferred as it clearly indicates that the type is an array.
It is an alternative form, which was borrowed from C, upon which java is based.
As a curiosity, there are three ways to define a valid main method in java:
public static void main(String[] args)
public static void main(String args[])
public static void main(String... args)
Both are equally valid. The int puzzle[] form is however discouraged, the int[] puzzle is preferred according to the coding conventions. See also the official Java arrays tutorial:
Similarly, you can declare arrays of other types:
byte[] anArrayOfBytes;
short[] anArrayOfShorts;
long[] anArrayOfLongs;
float[] anArrayOfFloats;
double[] anArrayOfDoubles;
boolean[] anArrayOfBooleans;
char[] anArrayOfChars;
String[] anArrayOfStrings;
You can also place the square brackets after the array's name:
float anArrayOfFloats[]; // this form is discouraged
However, convention discourages this form; the brackets identify the array type and should appear with the type designation.
Note the last paragraph.
I recommend reading the official Sun/Oracle tutorials rather than some 3rd party ones. You would otherwise risk end up in learning bad practices.
There is no difference, but Sun recommends putting it next to the type as explained here
The most preferred option is int[] a - because int[] is the type, and a is the name.
(your 2nd option is the same as this, with misplaced space)
Functionally there is no difference between them.
The Java Language Specification says:
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[][];
Thus they will result in exactly the same byte code.
In Java, these are simply different syntactic methods of saying the same thing.
They're the same. One is more readable (to some) than the other.
They are completely equivalent. int [] array is the preferred style. int array[] is just provided as an equivalent, C-compatible style.
Both have the same meaning. However, the existence of these variants also allows this:
int[] a, b[];
which is the same as:
int[] a;
int[][] b;
However, this is horrible coding style and should never be done.
There is no difference in functionality between both styles of declaration. Both declare array of int.
But int[] a keeps type information together and is more verbose so I prefer it.
They are the same, but there is an important difference between these statements:
// 1.
int regular, array[];
// 2.
int[] regular, array;
in 1. regular is just an int, as opposed to 2. where both regular and array are arrays of int's.
The second statement you have is therefore preferred, since it is more clear. The first form is also discouraged according to this tutorial on Oracle.
As already stated, there's no much difference (if you declare only one variable per line).
Note that SonarQube treats your second case as a minor code smell:
Array designators "[]" should be on the type, not the variable (squid:S1197)
Array designators should always be located on the type for better code
readability. Otherwise, developers must look both at the type and the
variable name to know whether or not a variable is an array.
Noncompliant Code Example
int matrix[][]; // Noncompliant
int[] matrix[]; // Noncompliant
Compliant Solution
int[][] matrix; // Compliant
Yep, exactly the same. Personally, I prefer
int[] integers;
because it makes it immediately obvious to anyone reading your code that integers is an array of int's, as opposed to
int integers[];
which doesn't make it all that obvious, particularly if you have multiple declarations in one line. But again, they are equivalent, so it comes down to personal preference.
Check out this page on arrays in Java for more in depth examples.
when declaring a single array reference, there is not much difference between them. so the following two declarations are same.
int a[]; // comfortable to programmers who migrated from C/C++
int[] a; // standard java notation
when declaring multiple array references, we can find difference between them. the following two statements mean same. in fact, it is up to the programmer which one is follow. but the standard java notation is recommended.
int a[],b[],c[]; // three array references
int[] a,b,c; // three array references
Both are ok. I suggest to pick one and stick with it. (I do the second one)
While the int integers[] solution roots in the C language (and can be thus considered the "normal" approach), many people find int[] integers more logical as it disallows to create variables of different types (i.e. an int and an array) in one declaration (as opposed to the C-style declaration).
Yes, there's a difference.
int[] a = new int[100]; // 'a' is not an array itself , the array is stored as an address
elsewhere in memory and 'a' holds only that address
int b[] = new int[100]; // while creating array like cleary shows 'b' is an array and it
is integer type.

different types of array declaration in java [duplicate]

I have recently been thinking about the difference between the two ways of defining an array:
int[] array
int array[]
Is there a difference?
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[][];
There isn't any difference between the two; both declare an array of ints. However, the former is preferred since it keeps the type information all in one place. The latter is only really supported for the benefit of C/C++ programmers moving to Java.
There is no real difference; however,
double[] items = new double[10];
is preferred as it clearly indicates that the type is an array.
It is an alternative form, which was borrowed from C, upon which java is based.
As a curiosity, there are three ways to define a valid main method in java:
public static void main(String[] args)
public static void main(String args[])
public static void main(String... args)
Both are equally valid. The int puzzle[] form is however discouraged, the int[] puzzle is preferred according to the coding conventions. See also the official Java arrays tutorial:
Similarly, you can declare arrays of other types:
byte[] anArrayOfBytes;
short[] anArrayOfShorts;
long[] anArrayOfLongs;
float[] anArrayOfFloats;
double[] anArrayOfDoubles;
boolean[] anArrayOfBooleans;
char[] anArrayOfChars;
String[] anArrayOfStrings;
You can also place the square brackets after the array's name:
float anArrayOfFloats[]; // this form is discouraged
However, convention discourages this form; the brackets identify the array type and should appear with the type designation.
Note the last paragraph.
I recommend reading the official Sun/Oracle tutorials rather than some 3rd party ones. You would otherwise risk end up in learning bad practices.
There is no difference, but Sun recommends putting it next to the type as explained here
The most preferred option is int[] a - because int[] is the type, and a is the name.
(your 2nd option is the same as this, with misplaced space)
Functionally there is no difference between them.
The Java Language Specification says:
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[][];
Thus they will result in exactly the same byte code.
In Java, these are simply different syntactic methods of saying the same thing.
They're the same. One is more readable (to some) than the other.
They are completely equivalent. int [] array is the preferred style. int array[] is just provided as an equivalent, C-compatible style.
Both have the same meaning. However, the existence of these variants also allows this:
int[] a, b[];
which is the same as:
int[] a;
int[][] b;
However, this is horrible coding style and should never be done.
There is no difference in functionality between both styles of declaration. Both declare array of int.
But int[] a keeps type information together and is more verbose so I prefer it.
They are the same, but there is an important difference between these statements:
// 1.
int regular, array[];
// 2.
int[] regular, array;
in 1. regular is just an int, as opposed to 2. where both regular and array are arrays of int's.
The second statement you have is therefore preferred, since it is more clear. The first form is also discouraged according to this tutorial on Oracle.
As already stated, there's no much difference (if you declare only one variable per line).
Note that SonarQube treats your second case as a minor code smell:
Array designators "[]" should be on the type, not the variable (squid:S1197)
Array designators should always be located on the type for better code
readability. Otherwise, developers must look both at the type and the
variable name to know whether or not a variable is an array.
Noncompliant Code Example
int matrix[][]; // Noncompliant
int[] matrix[]; // Noncompliant
Compliant Solution
int[][] matrix; // Compliant
Yep, exactly the same. Personally, I prefer
int[] integers;
because it makes it immediately obvious to anyone reading your code that integers is an array of int's, as opposed to
int integers[];
which doesn't make it all that obvious, particularly if you have multiple declarations in one line. But again, they are equivalent, so it comes down to personal preference.
Check out this page on arrays in Java for more in depth examples.
when declaring a single array reference, there is not much difference between them. so the following two declarations are same.
int a[]; // comfortable to programmers who migrated from C/C++
int[] a; // standard java notation
when declaring multiple array references, we can find difference between them. the following two statements mean same. in fact, it is up to the programmer which one is follow. but the standard java notation is recommended.
int a[],b[],c[]; // three array references
int[] a,b,c; // three array references
Both are ok. I suggest to pick one and stick with it. (I do the second one)
While the int integers[] solution roots in the C language (and can be thus considered the "normal" approach), many people find int[] integers more logical as it disallows to create variables of different types (i.e. an int and an array) in one declaration (as opposed to the C-style declaration).
Yes, there's a difference.
int[] a = new int[100]; // 'a' is not an array itself , the array is stored as an address
elsewhere in memory and 'a' holds only that address
int b[] = new int[100]; // while creating array like cleary shows 'b' is an array and it
is integer type.

Declaring arrays [duplicate]

I have recently been thinking about the difference between the two ways of defining an array:
int[] array
int array[]
Is there a difference?
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[][];
There isn't any difference between the two; both declare an array of ints. However, the former is preferred since it keeps the type information all in one place. The latter is only really supported for the benefit of C/C++ programmers moving to Java.
There is no real difference; however,
double[] items = new double[10];
is preferred as it clearly indicates that the type is an array.
It is an alternative form, which was borrowed from C, upon which java is based.
As a curiosity, there are three ways to define a valid main method in java:
public static void main(String[] args)
public static void main(String args[])
public static void main(String... args)
Both are equally valid. The int puzzle[] form is however discouraged, the int[] puzzle is preferred according to the coding conventions. See also the official Java arrays tutorial:
Similarly, you can declare arrays of other types:
byte[] anArrayOfBytes;
short[] anArrayOfShorts;
long[] anArrayOfLongs;
float[] anArrayOfFloats;
double[] anArrayOfDoubles;
boolean[] anArrayOfBooleans;
char[] anArrayOfChars;
String[] anArrayOfStrings;
You can also place the square brackets after the array's name:
float anArrayOfFloats[]; // this form is discouraged
However, convention discourages this form; the brackets identify the array type and should appear with the type designation.
Note the last paragraph.
I recommend reading the official Sun/Oracle tutorials rather than some 3rd party ones. You would otherwise risk end up in learning bad practices.
There is no difference, but Sun recommends putting it next to the type as explained here
The most preferred option is int[] a - because int[] is the type, and a is the name.
(your 2nd option is the same as this, with misplaced space)
Functionally there is no difference between them.
The Java Language Specification says:
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[][];
Thus they will result in exactly the same byte code.
In Java, these are simply different syntactic methods of saying the same thing.
They're the same. One is more readable (to some) than the other.
They are completely equivalent. int [] array is the preferred style. int array[] is just provided as an equivalent, C-compatible style.
Both have the same meaning. However, the existence of these variants also allows this:
int[] a, b[];
which is the same as:
int[] a;
int[][] b;
However, this is horrible coding style and should never be done.
There is no difference in functionality between both styles of declaration. Both declare array of int.
But int[] a keeps type information together and is more verbose so I prefer it.
They are the same, but there is an important difference between these statements:
// 1.
int regular, array[];
// 2.
int[] regular, array;
in 1. regular is just an int, as opposed to 2. where both regular and array are arrays of int's.
The second statement you have is therefore preferred, since it is more clear. The first form is also discouraged according to this tutorial on Oracle.
As already stated, there's no much difference (if you declare only one variable per line).
Note that SonarQube treats your second case as a minor code smell:
Array designators "[]" should be on the type, not the variable (squid:S1197)
Array designators should always be located on the type for better code
readability. Otherwise, developers must look both at the type and the
variable name to know whether or not a variable is an array.
Noncompliant Code Example
int matrix[][]; // Noncompliant
int[] matrix[]; // Noncompliant
Compliant Solution
int[][] matrix; // Compliant
Yep, exactly the same. Personally, I prefer
int[] integers;
because it makes it immediately obvious to anyone reading your code that integers is an array of int's, as opposed to
int integers[];
which doesn't make it all that obvious, particularly if you have multiple declarations in one line. But again, they are equivalent, so it comes down to personal preference.
Check out this page on arrays in Java for more in depth examples.
when declaring a single array reference, there is not much difference between them. so the following two declarations are same.
int a[]; // comfortable to programmers who migrated from C/C++
int[] a; // standard java notation
when declaring multiple array references, we can find difference between them. the following two statements mean same. in fact, it is up to the programmer which one is follow. but the standard java notation is recommended.
int a[],b[],c[]; // three array references
int[] a,b,c; // three array references
Both are ok. I suggest to pick one and stick with it. (I do the second one)
While the int integers[] solution roots in the C language (and can be thus considered the "normal" approach), many people find int[] integers more logical as it disallows to create variables of different types (i.e. an int and an array) in one declaration (as opposed to the C-style declaration).
Yes, there's a difference.
int[] a = new int[100]; // 'a' is not an array itself , the array is stored as an address
elsewhere in memory and 'a' holds only that address
int b[] = new int[100]; // while creating array like cleary shows 'b' is an array and it
is integer type.

What is the preferred way to declare a Java array? [duplicate]

I have recently been thinking about the difference between the two ways of defining an array:
int[] array
int array[]
Is there a difference?
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[][];
There isn't any difference between the two; both declare an array of ints. However, the former is preferred since it keeps the type information all in one place. The latter is only really supported for the benefit of C/C++ programmers moving to Java.
There is no real difference; however,
double[] items = new double[10];
is preferred as it clearly indicates that the type is an array.
It is an alternative form, which was borrowed from C, upon which java is based.
As a curiosity, there are three ways to define a valid main method in java:
public static void main(String[] args)
public static void main(String args[])
public static void main(String... args)
Both are equally valid. The int puzzle[] form is however discouraged, the int[] puzzle is preferred according to the coding conventions. See also the official Java arrays tutorial:
Similarly, you can declare arrays of other types:
byte[] anArrayOfBytes;
short[] anArrayOfShorts;
long[] anArrayOfLongs;
float[] anArrayOfFloats;
double[] anArrayOfDoubles;
boolean[] anArrayOfBooleans;
char[] anArrayOfChars;
String[] anArrayOfStrings;
You can also place the square brackets after the array's name:
float anArrayOfFloats[]; // this form is discouraged
However, convention discourages this form; the brackets identify the array type and should appear with the type designation.
Note the last paragraph.
I recommend reading the official Sun/Oracle tutorials rather than some 3rd party ones. You would otherwise risk end up in learning bad practices.
There is no difference, but Sun recommends putting it next to the type as explained here
The most preferred option is int[] a - because int[] is the type, and a is the name.
(your 2nd option is the same as this, with misplaced space)
Functionally there is no difference between them.
The Java Language Specification says:
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[][];
Thus they will result in exactly the same byte code.
In Java, these are simply different syntactic methods of saying the same thing.
They're the same. One is more readable (to some) than the other.
They are completely equivalent. int [] array is the preferred style. int array[] is just provided as an equivalent, C-compatible style.
Both have the same meaning. However, the existence of these variants also allows this:
int[] a, b[];
which is the same as:
int[] a;
int[][] b;
However, this is horrible coding style and should never be done.
There is no difference in functionality between both styles of declaration. Both declare array of int.
But int[] a keeps type information together and is more verbose so I prefer it.
They are the same, but there is an important difference between these statements:
// 1.
int regular, array[];
// 2.
int[] regular, array;
in 1. regular is just an int, as opposed to 2. where both regular and array are arrays of int's.
The second statement you have is therefore preferred, since it is more clear. The first form is also discouraged according to this tutorial on Oracle.
As already stated, there's no much difference (if you declare only one variable per line).
Note that SonarQube treats your second case as a minor code smell:
Array designators "[]" should be on the type, not the variable (squid:S1197)
Array designators should always be located on the type for better code
readability. Otherwise, developers must look both at the type and the
variable name to know whether or not a variable is an array.
Noncompliant Code Example
int matrix[][]; // Noncompliant
int[] matrix[]; // Noncompliant
Compliant Solution
int[][] matrix; // Compliant
Yep, exactly the same. Personally, I prefer
int[] integers;
because it makes it immediately obvious to anyone reading your code that integers is an array of int's, as opposed to
int integers[];
which doesn't make it all that obvious, particularly if you have multiple declarations in one line. But again, they are equivalent, so it comes down to personal preference.
Check out this page on arrays in Java for more in depth examples.
when declaring a single array reference, there is not much difference between them. so the following two declarations are same.
int a[]; // comfortable to programmers who migrated from C/C++
int[] a; // standard java notation
when declaring multiple array references, we can find difference between them. the following two statements mean same. in fact, it is up to the programmer which one is follow. but the standard java notation is recommended.
int a[],b[],c[]; // three array references
int[] a,b,c; // three array references
Both are ok. I suggest to pick one and stick with it. (I do the second one)
While the int integers[] solution roots in the C language (and can be thus considered the "normal" approach), many people find int[] integers more logical as it disallows to create variables of different types (i.e. an int and an array) in one declaration (as opposed to the C-style declaration).
Yes, there's a difference.
int[] a = new int[100]; // 'a' is not an array itself , the array is stored as an address
elsewhere in memory and 'a' holds only that address
int b[] = new int[100]; // while creating array like cleary shows 'b' is an array and it
is integer type.

Categories