6

I was watching a video, and they showed that they were establishing a float array like this:

private final float x[];

I have always done this:

private final float[] x;

I tested both and neither produces an error. Is there a difference or is this just preference?

Zach_1919
  • 107
  • 6

2 Answers2

4

From the JLS:

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[][];

There's no difference.

user2357112
  • 260,549
  • 28
  • 431
  • 505
2

No difference, the first syntax is just a C-like way to declare an array and the second was introduced 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 it is not a good practice to do that in my opinion.

Dici
  • 25,226
  • 7
  • 41
  • 82