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?
In Java, all array elements are automatically initialized to the default value. For primitive numerical types, that's 0 or 0.0 .
Yes, for primitive types(except boolean and char) it will be default to ZERO.
If you want to initialize an array, try using Array Initializer: int[] data = {10,20,30,40,50,60,71,80,90,91}; // or int[] data; data = new int[] {10,20,30,40,50,60,71,80,90,91}; Notice the difference between the two declarations. When assigning a new array to a declared variable, new must be used.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With