我用 Java 编写了这段代码,我在想这样做有什么区别:String [] whatever
和 String whatever []
,如果有人能告诉我的话发生了什么,最好使用 String [] whatever
= {} 或使用 String whatever[]
= {} 否则它只是被弃用了。
public class Snippet136
{
public static void main(String [] args)
{
String names[] = {"Jhonny", "Edurardo", "Francis", "Franklin", "Freedy"};
String [] eyes_colors = { "blue", "red", "black", "green", "black"};
System.out.print("Names:");
for (String name: names) {
System.out.print( " " + name );
}
System.out.print("\n\nEyes colors:");
for ( String eyes_color: eyes_colors ) {
System.out.print( " " + eyes_color );
}
}
}
最佳答案
什么都没有:这两种形式是相同的。
String[] var
更自然,通常更受欢迎(因为它更符合逻辑地将数组类型与类型名称结合起来)。 String var[]
在 Java 中基本上是因为 C/C++ 使用该语法,因此它是一种“语法兼容性”。
请注意,后缀 []
会导致一些非常疯狂的语法特性,例如可以编写 int function()[] { return null; }
:返回 int[]
的函数。
关于java - String name[] = {}; 有什么区别?和字符串 [] 名称 = {};,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28596610/