Inconsistency in returning primitive arrays in Java

The following show different ways of instantiating and returning a primitive array. However, for some reason, the last one doesn't work. Is there a valid explanation for this inconsistency? Why doesn't the last block work?

Block 1

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

Block 2

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

Block 3

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

Block 4

    return {50};   // doesn't work
Jon Skeet
people
quotationmark

Why doesn't the last block work?

Because an array initializer (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), as per your third block.

Your fourth block isn't either a variable declaration or an array creation expression, so it's not valid.

Note that this isn't specific to primitive arrays at all - it's the same for all arrays.

people

See more on this question at Stackoverflow