为什么数组只能在赋值时创建而不用`new`?

Why can arrays only be created without `new` during assignment?

在分配类型 T 数组期间,表达式的 new T[] 部分是隐式的。

int[] test = {1,2,3,5};

语言规范是否说明了为什么其他地方不支持这种类型的初始化?像下面这样的语法会与任何其他语言规则冲突吗?看起来类型推断不会太难。

public static void test(int[] a, int[] b) { ... };
... 
test({1,2,3}, {4,5,6});

JLS 确实说您不能在第 15 节中执行此操作。从 15.12 Method Invocation Expressions we can see all the valid ways in which you may invoke a method. The important part of that is the optional ArgumentList 开始,将给出有关参数有效语法的规范。

在调用表达式下面我们可以看到 ArgumentList, which is mainly just another link to Expression.

的定义

当我们跟随它时,我们可以看到 Expression; LambdaExpression and AssignmentExpression. As far as I can tell your question is of the "Class instance creation expressions" 类型中定义了两种类型的表达式。 foo(new Bar[]{new Bar(), new Bar()}); 之类的调用是有效的这一事实证明了这一点。

当我们按照 link 到第 15.9 节时,我们可以看到您提议的内容根本不受支持。

这并没有回答为什么;我确实意识到这一点。但是,只有做出该决定的人才能回答原因。我们知道这种狡猾是可能,其他语言也证明了这一点。