我经常看到数组被这样初始化:
String[] array = new String[] { "foo", "bar", "baz" };
但是阅读语言基础-数组可以发现,短语法不需要显式实例化构造函数:
Alternatively, you can use the shortcut syntax to create and initialize an array:
int[] anArray = { 100, 200, 300, 400, 500, 600, 700, 800, 900, 1000 };
因此,假设有这两种初始化方法:
String[] array = new String[] { "foo", "bar", "baz" };
String[] array2 = { "foo", "bar", "baz" };
这两者之间有什么区别吗?它们似乎都能正常工作,那么我应该假设第二个隐式调用了
new String[]
,第一个只是更冗长的方式,还是背后有更多的东西?
从Java开始,如果这个问题太蠢,请原谅,但我在网上找不到任何关于这个问题的信息。
=]
- Fabrício Matté