8

I saw someone initialize and array like this in java

int[] s;

s = new int[]{ and put the list here..}

versus

int[] s = { the list here} 

Are these both acceptable way of doing it?

3 Answers 3

8

Yes, both are equally valid ways of creating a java integer array. The second version is just a shortcut syntax of the first version.

More on that here : http://download.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html

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

Comments

8

Yes, the latter is a shorthand for the former in a specific case: The latter can only be used directly in an initializer of a variable (where the type is given directly on the left-hand side), whereas the former can be used as an expression in general.

1 Comment

+1 for mentioning the limitation of using the shortcut syntax.
0

Yes, the actual bytecode generated in both cases is exactly the same.

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.