int[] a = new int[] {1, 2, 3};
和
int[] a = {1, 2, 3};
这些之间是否存在实际差异?
答案 0 :(得分:2)
答案 1 :(得分:0)
你的第二种形式只是第一种形式的句法简写。它们编译成完全相同的字节码。
答案 2 :(得分:0)
从编译的类文件中查看字节码,没有区别。
public class XFace {
public void test1(){
int[] a = new int[] {1, 2, 3};
}
public void test2(){
int[] a = {1, 2, 3};
}
}
Compiled from "XFace.java"
public class XFace extends java.lang.Objec
public XFace();
Code:
0: aload_0
1: invokespecial #8; //Method java/
4: return
public void test1();
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 test2();
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
}