10

What is the difference between the two following methods of array initialization:

  1. Object[] oArr = new Object[] {new Object(), new Object()};
  2. Object[] oArr = {new Object(), new Object()};

Is it related to heap/stack allocation?

Thanks!

7
  • 8
    There' no difference. Commented Jan 16, 2011 at 17:21
  • "premature optimization is the root of all evil" Commented Jan 16, 2011 at 17:27
  • @ Carl Manaster: this should be your answer. Commented Jan 16, 2011 at 17:29
  • Thanks, @Naveed, but it's too short for an answer. Commented Jan 16, 2011 at 17:37
  • 5
    @gulbrandr: It would only be premature optimization if the OP believed them to do the same thing, and was asking which one was faster. It's not an "optimization" question when he's asking whether there are any differences between the two - if they behave differently, that's far more important than optimization. Commented Jan 16, 2011 at 17:39

4 Answers 4

22

None at all - they're just different ways of expressing the same thing.

The second form is only available in a variable declaration, however. For example, you cannot write:

foo.someMethod({x, y});

but you can write:

foo.someMethod(new SomeType[] { x, y });

The relevant bit of the Java language specification is section 10.6 - Array Initializers:

An array initializer may be specified in a declaration, or as part of an array creation expression (§15.10), creating an array and providing some initial values:

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

7 Comments

@gasan: Yes, that's part of an array creation expression... I don't see how that goes against anything in my answer.
I think it would be better to add that inside a method, return new SomeType[] {x, y}; is valid but return {x, y}; is invalid.
@PriyanshulGovil: I don't see why it's worth highlighting return statements specifically. I've already given one example, as well as the general case of the second form being available in a variable declaration. A return statement is not a variable declaration, therefore it's invalid in that context.
Oh, I was seeing another question in parallel, which asked the difference between both new SomeType[] {..} and {..} (not specifically during assignment), and I commented on the wrong question. I'm sorry.
Although, SomeType object = SomeFunction() is still initialization of object from a function which returns SomeType.
|
2

Absolutely identical. The second is allowed shorthand for the first (only when, as here, it is done as part of a variable declaration.

Comments

1

In Java all objects live in the heap, as arrays are objects in Java they lives in the stack.

for these two there is no difference in result, you 'll got two array objects with the same elements.

However sometimes you will encounter some situations where you can't use them, for example you don't know the elements of the array. then you get stuck with this form:

Object [] array=new Object[size];

Comments

0

There is a small and catchy difference still!

You can do

int[] arr;
arr= {1,2,3}; // Illegal

But you can very well do

int[] arr;
arr = new [] {1,2,3} //Legal

Also if you are to initialize later then you cannot do

int arr;
arr = new [] {1,2,3} //Illegal

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.