Very related, so check it out: Java: what's the big-O time of declaring an array of size n?
The resulting bytecode for initializing an array involves the newarray instruction, which is O(n) as each element is initialized to the default state before anything else. See: JVM Spec., page 576:
[...] Each of the elements of the new array is initialized to the defaultinitial value (§2.3, §2.4) for the element type of the array type.
What you are using here additionally is an array initializer, so let's check out the bytecode:
public class Main {
public static void main(String[] args) {
int[] array = {1, 2 , 3, 4, 5};
}
}
... is compiled to ...
public class Main {
public Main();
Code:
0: aload_0
1: invokespecial #1 // Method java/lang/Object."<init>":()V
4: return
public static void main(java.lang.String[]);
Code:
0: iconst_5
1: newarray int
3: dup
4: iconst_0
5: iconst_1
6: iastore
7: dup
8: iconst_1
9: iconst_2
10: iastore
11: dup
12: iconst_2
13: iconst_3
14: iastore
15: dup
16: iconst_3
17: iconst_4
18: iastore
19: dup
20: iconst_4
21: iconst_5
22: iastore
23: astore_1
24: return
}
Notice the newarray with iconst_5 passed to it. This belongs to the "new int[5] part" of the code, where iconst_5 is just 5 ("integer constant 5"). Ignore the dup and astore_1, they're not so important for the question.
Notice the iconst_0, iconst_1 and iastore block, which is repeated for other integer constants. iastore is the instruction to store a value in an array, where the first parameter is the index and the second parameter is the value. Two lines before the iastore are the arguments. So what you see is that the bytecode reads each of the elements in your array initializer and stores it in the array.
So what the one-liner does is actually very similar (or equivalent) to this:
public class Main {
public static void main(String[] args) {
int[] array = new int[5];
array[0] = 1;
array[1] = 2;
array[2] = 3;
array[3] = 4;
array[4] = 5;
}
}
As you can see, the one-liner you've shown is actually 2 * O(n) at bytecode-level.