int[] a = new int[] {1, 2, 3};
和
int[] a = {1, 2, 3};
这两者之间有什么实际差别吗?
int[] a = new int[] {1, 2, 3};
和
int[] a = {1, 2, 3};
这两者之间有什么实际差别吗?
String a = "hello";
和 String a = new String("Hello");
是不同的。 - Maroun查看编译后的类文件的字节码,没有任何区别。
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
}
你的第二个表单只是第一个表单的语法简写。它们编译成完全相同的字节码。