Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any difference between "Object[] x" and "Object x[]"?

I was updating a legacy code base in Java and I found a line like this:

Object arg[] = { new Integer(20), new Integer(22) };

That line catched my attention because I am used to this kind of code:

Object[] arg = { new Integer(20), new Integer(22) };

The content of the array isn't important here. I'm curious about the brackets next to the variable name versus the brackets next to the class name. I tried in Eclipse (with Java 5) and both lines are valid for the compiler.

Is there any difference between those declarations?

like image 824
JuanZe Avatar asked Feb 09 '10 14:02

JuanZe


People also ask

What does Object [] mean Java?

A Java object is a member (also called an instance) of a Java class. Each object has an identity, a behavior and a state.

What is {} and [] in Javascript?

{} is shorthand for creating an empty object. You can consider this as the base for other object types. Object provides the last link in the prototype chain that can be used by all other objects, such as an Array . [] is shorthand for creating an empty array.


1 Answers

Both are legal and both work. But placing [] before the array's name is recommended.

From Javadocs:

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.

like image 127
codaddict Avatar answered Oct 05 '22 01:10

codaddict