新int [] {}之间的区别或者只是{}
这两行代码之间是否存在显着差异?新int [] {}之间的区别或者只是{}
int[] array = new int[]{1,2,3} int[] array = {1,2,3} 
如果我不得不猜测,相同的构造函数在第二个版本中隐式调用,使它们相同。
编辑: This question was explored previously here but with default values.我的问题考虑了非默认值的数组的初始化。
回答:
只是出于好奇,我也去编译类以下方法
public void arrayTest1(){     int[] array = new int[]{1,2,3}; 
} 
public void arrayTest2(){ 
    int[] array = {1,2,3}; 
} 
下面是与之相关的结果。
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 
这两个语句本质上看起来是相同的反编译。
以上是 新int [] {}之间的区别或者只是{} 的全部内容, 来源链接: utcz.com/qa/260717.html
