Java 中返回原始数组的不一致

Inconsistency in returning primitive arrays in Java

下面展示了实例化和返回原始数组的不同方式。但是,由于某种原因,最后一个不起作用。对于这种不一致是否有有效的解释?为什么最后一个区块不起作用?

区块 1

    int[] a = new int[] {50};
    return a;    // works fine

区块 2

    int[] a = {50};
    return a;    // works fine

区块 3

    return new int[] {50};    // works fine

区块 4

    return {50};   // doesn't work

Why doesn't the last block work?

因为数组初始值设定项 (JLS 10.6) is only valid in either a variable declaration, as per your first and second blocks, or as part of an array creation expression (JLS 15.10.1),根据您的第三个块。

你的第四个块既不是变量声明也不是数组创建表达式,所以它是无效的。

请注意,这根本不是原始数组特有的 - 所有 数组都一样。