初始化一个简单数组的这2个方面的区别 [英] difference between these 2 ways of initializing an simple array
本文介绍了初始化一个简单数组的这2个方面的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在Java中,我可以初始化predefined内容或者通过数组:
In java, I can initialize an array with predefined content either by :
int[] myArr = new int[]{1,2,3};
或
int[] myArr = {1,2,3};
从本质上讲,是有这两种方式有什么区别?他们是在Java中完全一致的?哪种方式更好,为什么?
Essentially, is there any difference between these two ways ? Are they completely identical in Java ? Which way is better and why?
推荐答案
在你的情况没有什么区别。
In your case there is no difference.
将有一个区别当你不分配你的阵列
变量和做在线创作。
There will be a difference when you are not assigning your array
to variable and doing inline creation.
例如,求索有方法,它接受一个数组作为参数。</ P>
for example, conside there is method, which takes an array as argument.
private void someX(int[] param){
// do something
}
您情况:
someX(myArr); // using some declared array .I.e your case
现在看到了差距,而在其他情况下调用它。
Now see the difference while calling it in other cases.
someX(new int[] {1,2,3}); // yes, compiler satisfied.
someX({1,2,3}); //Error. Sorry boss, I don't know the type of array
这篇关于初始化一个简单数组的这2个方面的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文