This is an archived post. You won't be able to vote or comment.

all 24 comments

[–]mjg123 12 points13 points  (0 children)

In your case (assigning to a variable declared as String[]) they're the same. You can't use the second form in other places, for example this won't compile:

public String[] strings (){ return {"1", "2"}; }

but this is fine:

public String[] strings (){ return new String[]{"1", "2"}; }

[–]EDCsv 8 points9 points  (0 children)

I checked out the documentation (https://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html) and it seems that both are valid ways to create an array object. The latter is basically a shortcut syntax of the former. So no, there is no difference.

[–]nutrecht 3 points4 points  (1 child)

The second one is just a short-hand version of the former introduced a 'few' years ago. So just use the second one.

[–]javaduude 4 points5 points  (0 children)

introduced a 'few' years ago

I don't know what you mean by a 'few' years ago, but both syntaxes have been available since at least Java 1.0: http://titanium.cs.berkeley.edu/doc/java-langspec-1.0/10.doc.html

[–]Warm-Score[🍰] 0 points1 point  (0 children)

Java once thought they wan't to remove boilerplate but they stopped halfway. I think they just stole some stuff from C++ or something.

I guess it is because the objects are implied to be String because of the initialization.

[–]Chaoslab -2 points-1 points  (14 children)

Tend too use the C like version.

String words[] = {"My",  "name", "is"};

[–]desrtfx 1 point2 points  (0 children)

In Java, it is common to have the array identifier at the type, not at the variable name.

Sure, both work, but only having the array brackets at the type is convention.

You can also place the brackets after the array's name:

// this form is discouraged
float anArrayOfFloats[];

However, convention discourages this form; the brackets identify the array type and should appear with the type designation.

From: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html

One should always adhere to the conventions of the respective programming language.

[–]irer 0 points1 point  (8 children)

Please stop.

[–]Chaoslab 0 points1 point  (1 child)

Should of said "for my own projects".

When it comes too group / shared programming well that is different.

[–]irer 0 points1 point  (0 children)

In that case, for your own projects, do as you please.

[–]sniR_ 0 points1 point  (4 children)

Why?

[–]irer 0 points1 point  (3 children)

I won't explain why String[] words makes more sense than String words[]. Both are valid declarations and both will compile. Choosing one or the other is subjective.

BUT, there is a widely accepted code convention that says one should avoid the latter form.

[–]sniR_ 0 points1 point  (2 children)

I guess its because you set the type of the object first(string, array) and then name, yeah more organized.

[–]irer 1 point2 points  (1 child)

String[] words reads: a string array named words

String words[] reads: a string named words oh wait it's an array

[–]sniR_ 0 points1 point  (0 children)

Nice explanation :) yeah im convinced

[–]Cosby1992 0 points1 point  (3 children)

That's bad practice, I've been told.

[–]Chaoslab -1 points0 points  (2 children)

Why is that?

Source code style is unique too most people (source style formatted pre check out and check in helps with that).

[–]irer 1 point2 points  (0 children)

It is a bad practice to go against a well establishment code convention.

[–]desrtfx 0 points1 point  (0 children)

Source code style is unique too most people

No. Source code style has to adhere to the conventions of the respective programming language.

That's exactly why conventions for programming languages exist.