What is the difference between float x [] and float [] x?

I watched the video and they showed that they set such a float array:

private final float x[]; 

I always did this:

 private final float[] x; 

I tested both and did not cause an error. Is there a difference or is it just a preference?

+6
source share
2 answers

From JLS :

[] can be displayed as part of the type at the beginning of the declaration, or as part of the declarator for a specific variable, or both.

For instance:

 byte[] rowvector, colvector, matrix[]; 

This declaration is equivalent to:

 byte rowvector[], colvector[], matrix[][]; 

There is no difference.

+3
source

It makes no difference, the first syntax is just a C-shaped way to declare an array, and the second with Java.

However, if you declare several variables on the same line, there is a difference:

 float[] a, b; 

declares 2 arrays whereas

 float a[], b; 

declares an array and a float, but in my opinion this is not a good practice.

+2
source

Source: https://habr.com/ru/post/975607/


All Articles