8

When I create a new array, I do something like this.

int[] anArray = {1, 2, 3};

But I've seen some people do something like.

int[] anArray = new int[] {1, 2, 3};

I understand what it does, but I don't understand the purpose of it. Is there a benefit of doing it one way over another?

Thanks

4 Answers 4

11

There's no difference in behaviour where both are valid. They are covered in section 10.6 and 15.10 of the Java language specification.

However the first syntax is only valid when declaring a variable. So for example:

 public void foo(String[] args) {}

 ...

 // Valid
 foo(new String[] { "a", "b", "c" };

 // Invalid
 foo({"a", "b", "c"});

As for the purpose - the purpose of the first syntax is to allow variable declarations to be more concise... and the purpose of the second syntax is for general-purpose use as an expression. It would be odd to disallow the second syntax for variable declarations, just because the more concise syntax is available.

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

4 Comments

I know that. I am only talking about declaring an array like in the OP.
@JonMannerberg: Well it's unclear how much you know a topic when you ask a question on it, and don't state which bits you do know.
@JonMannerberg : There is no better answer than one from JON SKEET himself
+1 - I learned something :-) never bothered to think about the purpose
5

One would use the second way, if the declaration should be done before the initialization.

int[] arr;
arr = { 2, 5, 6, 12, 13 };

Would not work, so you use:

int[] arr;
arr = new int[]{ 2, 5, 6, 12, 13 };

instead. Another way is to declare another variable:

int[] arr;
int[] tmparr = { 2, 5, 6, 12, 13 };
arr = tmparr;

So if you don't need to seperate the declaration from the initialization, it's only a matter of clean code (use either one consistently).

1 Comment

From these posts, I am getting that it is merely personal preference, is this right?
5

They are almost the same thing, but the first is applicable for object assignment like:

int[] anArray = {1, 2, 3};

The other one is more globaly like

callingMyMethod(new Object[]{object1,object2});

The wrong syntax would be

callingMyMethod({object1,object2});

Let's take it further

These initialization are right:

Object[] objeto={new Object(), new Object()};
Object[] objeto=new Object[]{new Object(), new Object()};

Also right:

Object[] objeto;
objeto=new Object[]{new Object(), new Object()}

But as Jon suggested this is wrong:

Object[] objeto;
objeto={new Object(), new Object()};

Why? Array Initializer And Array Creation Expression

Anyway both of your syntax are correct. There is no benefit on one against the other.

Interesting reading on this subject:

Arrays on Oracle Official documentation

This have also been covered on this thread

5 Comments

The first syntax is fine for reference types too, so long as you have the values beforehand.
Still incorrect: it's not assignment which is important, it's whether the value is part of a variable declaration. You can't use the first syntax to assign a new value to a previously-declared variable.
but you can assign it on the declaration
Yes, the assignment is part of the declaration - but it's the fact that it's part of a declaration which is the important part. If you try to use it in an assignment to an existing variable, it's invalid... so your claim that "the first is applicable for object assignment" is incorrect.
A did it a little bit more explicative of what I meant
5

It is just a standard. If you use:

  int[] anArray = new int[] {1, 2, 3};

You are saying that each number (1,2,3) it's an int. It could not be double, float, etc. It's just for security reason.

Comments

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.