Is there a significant difference between these two lines of code?
int[] array = new int[]{1,2,3}
int[] array = {1,2,3}
If I had to guess, the same constructor is called implicitly in the second version, making them identical.
Edit: This question was explored previously here but with default values. My question regarded the initialization of an array with non-default values.
Just out of curiosity, I did de-compile the class with following methods
public void arrayTest1(){
int[] array = new int[]{1,2,3};
}
public void arrayTest2(){
int[] array = {1,2,3};
}
Here is the result related to them.
public void arrayTest1();
Code:
0: iconst_3
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: astore_1
16: return
public void arrayTest2();
Code:
0: iconst_3
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: astore_1
16: return
Both the statements essentially look the same de-compiled.
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