开发者

Java arrays initializing after declare

开发者 https://www.devze.com 2023-04-10 17:02 出处:网络
I saw someone initialize and array like this in java int[] s; s = new int[]{ and put the list here..} versus

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

精彩评论

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