Initialize array of primitives Initialize array of primitives arrays arrays

Initialize array of primitives


There is none, they produce exactly the same bytecode. I think it may be that the second form wasn't supported in older versions of Java, but that would have been a while back.

That being the case, it becomes a matter of style, which is a matter of personal preference. Since you specifically asked, I prefer the second, but again, it's a matter of personal taste.


As others have mentioned, they are equivalent and the second option is less verbose. Unfortunately the compiler isn't always able to understand the second option:

public int[] getNumbers() {   return {1, 2, 3}; //illegal start of expression}

In this case you have to use the full syntax:

public int[] getNumbers() {   return new int[]{1, 2, 3};}


There is no difference between the two statements.Personally speaking, the second one is preferred. Because you have all the elements specified in the braces. The compiler will help you to compute the size of the array.

So no need to add int[] after the assignment operator.