I often see arrays being initialized like this:
String[] array = new String[] { "foo", "bar", "baz" };
But reading the Language Basics - Arrays shows that the short syntax doesn't require explicitly instancing the constructor:
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
};
So, assuming these two methods of initialization:
String[] array = new String[] { "foo", "bar", "baz" };
String[] array2 = { "foo", "bar", "baz" };
Is there any difference between these? Both seems to work the same, in that case should I assume that the second one implicitly calls the new String[]
and the first one is just a more verbose way, or is there more to it behind the scenes?
Starting with Java so sorry if this is way too stupid of a question, but I couldn't find anything about this in the web.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…