Initialize array of primitives
Asked Answered
S

7

26

I am wondering,

What's exactly the difference between these two ways of initializing an array of primitives:

int[] arr1 = new int[]{3,2,5,4,1};

int[] arr2 = {3,2,5,4,1};

and which one is preferred ?

Stewardson answered 27/5, 2012 at 15:41 Comment(2)
There is no difference. Stop giving importance to such tiny details and focus on the bigger picture!Ineslta
The second; what's the point of the first?Coronation
H
19

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.

Hennahane answered 27/5, 2012 at 15:43 Comment(2)
What version was this feature added?Unsnarl
@reesjones: A long time ago, anything even vaguely modern has it.Hennahane
A
9

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};
}
Amidst answered 27/5, 2012 at 16:5 Comment(0)
C
7

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.

Castanon answered 27/5, 2012 at 15:44 Comment(0)
C
4

In your case, these two styles end up same effect, both correct, with the second one more concise. But actually these two styles are different.

Remember arrays in java are fixed length data structures. Once you create an array, you have to specify the length.

Without initialization, the first case is

int[] arr1 = new int[5];

The second case it would be

int[] arr2 = {0,0,0,0,0};

You see the difference? In this situation, the first style is preferred as you don't have to type all those default initial values manually.

To me, the only big difference between the two styles is when creating an array without explicit initialization.

Carsoncarstensz answered 10/9, 2013 at 21:17 Comment(0)
I
1

In this case, the second one because it's prettier and less verbose :)

Ineslta answered 27/5, 2012 at 15:45 Comment(0)
I
1

useful in this situation

void foo(int[] array) {

}

calling with a literal

// works
foo(new int[]{5, 7})

//illegal
foo({5, 7})
Inhospitality answered 27/5, 2012 at 17:37 Comment(0)
H
0

Adding to @Paul Bellora's answer, only the second option will work if you're trying to initialize a primitive array using ternary operator

int[] arr1 = false ? {} : {1,2}; // doesn't work
int[] arr2 = false ? new int[]{} : new int[]{1,2}; // works
Highball answered 6/4, 2021 at 15:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.