为什么我不能使用{"a","b"}作为String数组方法参数的输入

Che*_*eng -1 java

我想知道,为什么我不能使用{"a","b"}作为String数组方法参数的输入?

public static void fun(String[] s) {
}

public static void main(String[] args) {
    String[] s = {"a", "b"};

    // OK
    fun(s);

    // This line is not accepted by compiler
    fun({"a", "b"});
}  
Run Code Online (Sandbox Code Playgroud)

Jon*_*eet 6

JLS第10.3节:

数组由数组创建表达式(第15.10节)或数组初始值设定项(第10.6节)创建.

数组初始化的语法只有效直接一个变量声明的时一部分.在其他任何地方 - 包括以后的分配 - 您必须使用数组创建表达式:

fun(new String[] { "a", "b" });
Run Code Online (Sandbox Code Playgroud)

我怀疑这基本上使语言更简单.