Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between float x[] vs. float[] x? [duplicate]

Tags:

java

arrays

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?

like image 702
Zach_1919 Avatar asked Sep 21 '14 16:09

Zach_1919


People also ask

Is there any difference between int [] A and int a [] in C?

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.

What is the difference between float and float?

Float is an object; float is a primitive. Same relationship as Integer and int , Double and double , Long and long . float can be converted to Float by autoboxing, e.g.

When to use float or int?

An integer (more commonly called an int) is a number without a decimal point. A float is a floating-point number, which means it is a number that has a decimal place. Floats are used when more precision is needed.

What is the difference between long and int?

The basic difference between the type int and long is of their width where int is 32 bit, and long is 64 bits. The types int and long when counted in bytes instead of bits the type int is 4 bytes and the type long is just twice if type int i.e. 8 bytes.


2 Answers

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.

like image 152
user2357112 supports Monica Avatar answered Oct 20 '22 08:10

user2357112 supports Monica


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.

like image 33
Dici Avatar answered Oct 20 '22 09:10

Dici