开发者

Java arrays initializing after declare

I saw someone initialize and array like this in java

int[] s;

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

versus

int[] s = {开发者_StackOverflow the list here} 

Are these both acceptable way of doing it?


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


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜