任何人都可以告诉我下面的数组初始化有什么区别。
String[] emptyName = new String[]{"hi","hello","what"};
String[] emptyName={"hi","hello","what"};
String emptyName[] = new String[]{"hi","hello","what"};
String emptyName[] = new String[]{new String("hi"),new String("hello"),new String("what")};
谢谢
最佳答案
没有什么区别。这只是一个Syntactic sugar
在数组声明中。
我更喜欢不太困惑的方式,即第二种。
等等...仔细看。现在您就会看到差异。下面的示例显示了初始化样式的重要性。
有一个方法
private void methodName(String[] strs){
// do something
}
打电话时,看看有什么区别。
methodName(new String[] {"hi","hello","what"}); // inline creation
methodName({"hi","hello","what"}); //Error. Type missing now
关于java - 不同类型的数组声明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27624848/