2

I'm currently bored and was doing some Java practice tests since it's been quite some time I've programmed in Java. After a certain question I'm now wondering the differences between the following:

String[] test1 = { "A", "B", "C" };
String[] test2 = new String[]{ "A", "B", "C" };
String test3[] = { "A", "B", "C" };
String test4[] = new String[]{ "A", "B", "C" };

When I type this in a Java IDE I'm not getting any errors. I haven't tried to compile any of them yet (although I've mainly used test2 myself in the past, so I know that one works).

  • So, does each of these compile?
  • And if yes, what are the differences (if any)?
  • (Is test1 better for performance than test2 because you don't make a new String[]-instantiation, or is this done anyway behind the scenes?)
  • (If test1 is the same as test3: Why does test3 and test4 exist in Java if it's the same as test1 and test2? Are there any cases where using [] behind the field-name is preferred above behind the type?)
  • (Are there more (almost similar) variants apart from these four?)
  • (And other questions like these.)

Just want to know out of curiosity. We learn something new every day I guess.

3
  • 1/3 and 2/4 are identical. 1/2/3/4 are identical. I don't know what you mean "why does test3 and test4 exist"--they exist because you created them. Commented Aug 19, 2014 at 13:54
  • @DaveNewton What I meant was, why does Java syntax allow this? Are there any programmers out there that even prefer using the [] after the field-name instead of the type? It's certainly new (and pretty counter-intuitive) for me personally.. Commented Aug 19, 2014 at 13:58
  • Because that's how C used to do it, and Java was supposed to feel familiar. It's the opposite of new. Commented Aug 19, 2014 at 14:10

2 Answers 2

5

They are all equivalent.

  • So, does each of these compile?

Yes, as you yourself can also verify.

  • And if yes, what are the differences (if any)?
  • (Is test1 better for performance than test2 because you don't make a new String[]-instantiation, or is this done anyway behind the scenes?)

There are no differences in compiled byte code.

  • (If test1 is the same as test3: Why does test3 and test4 exist if it's the same as test1 and test2?)

It is just an alternative way to write it as the array indices can be put after the type name as well as after the identifier name. Only difference is if you declare multiple variable in one line, if indices are placed after the type name, all listed identifiers will be an array of that type, while if the indices are put after the identifier name, only that identifier will be an array.

Example:

String[] array1, array2;    // Both are arrays
String array3[], notArray4; // Only array3 is array

I recommend putting indices after the type (String[] array1) so it will be unambigous.

(Are there more (almost similar) variants apart from these four?)

Not that I know of.

Sign up to request clarification or add additional context in comments.

2 Comments

Ah ok, now it makes more sense why using [] after the field-name exists in Java. Thanks, will accept this as the answer as soon as I can.
And about your line "I recommend putting indices after the type (String[] array1) so it will be unambigous."; I will indeed keep using this. I was just wondering what the differences were and why [] after the field-name exist, which you've answered perfectly.
1

The preferred way to declare arrays is to attach the [] to the type. So String[] instead of String test3[]. That takes care of the last 2.

String allows you to write the shortcut way of String[] test1 = { "A", "B", "C" };, without adding the new String[].

So in conclusion, the best way according to code conventions and least code to type is your first choice: String[] test1 = { "A", "B", "C" };.

All are legal syntax though.

2 Comments

I know adding the [] to the type is preferred, not only by me but in general for programmers. And I also know test1 is the shortest.. I just wanted to know what the differences are. Why does test3 and test4 exist in the first place and are they doing the same as test1 and test2?
All are legal syntax and generate identical bytecode.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.