英文:
Is there a difference between new Class[]{} and new Class[0]?
问题
有new Class[]{}
和new Class[0]
之间有区别吗?如果有,有什么不同?
英文:
Is there a difference between new Class[]{}
and new Class[0]
? If yes, what is different?
答案1
得分: 6
没有区别。
反编译这段代码:
final class Foo {
Class[] a = new Class[]{};
Class[] b = new Class[0];
}
得到的结果是:
final class Foo {
java.lang.Class[] a;
java.lang.Class[] b;
Foo();
Code:
0: aload_0
1: invokespecial #1 // Method java/lang/Object."<init>":()V
4: aload_0
5: iconst_0
6: anewarray #2 // class java/lang/Class
9: putfield #3 // Field a:[Ljava/lang/Class;
12: aload_0
13: iconst_0
14: anewarray #2 // class java/lang/Class
17: putfield #4 // Field b:[Ljava/lang/Class;
20: return
}
也就是说,这两种形式生成的字节码是完全相同的。
英文:
No difference.
Decompiling this:
final class Foo {
Class[] a = new Class[]{};
Class[] b = new Class[0];
}
yields
final class Foo {
java.lang.Class[] a;
java.lang.Class[] b;
Foo();
Code:
0: aload_0
1: invokespecial #1 // Method java/lang/Object."<init>":()V
4: aload_0
5: iconst_0
6: anewarray #2 // class java/lang/Class
9: putfield #3 // Field a:[Ljava/lang/Class;
12: aload_0
13: iconst_0
14: anewarray #2 // class java/lang/Class
17: putfield #4 // Field b:[Ljava/lang/Class;
20: return
}
That is, the two forms yield identical bytecode.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论