Initializing Java arrays after declaration

I saw someone initialize and arrays like this in java

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

against

 int[] s = { the list here} 

Are they both acceptable ways to do this?

+7
source share
3 answers

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

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

+7
source

Yes, the latter is an abbreviation for the former in a particular case: the latter can only be used directly in the variable initializer (where the type is set directly on the left side), while the former can be used as an expression as a whole.

+8
source

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

0
source

All Articles