Strange array return type

JavaArraysSyntaxDeclaration

Java Problem Overview


Has any one seen the array [] placed after the method signature like this?

public static String mySplit(String s)[] {
    return s.split(",");
}

public static void main(String... args) {
    String[] words = mySplit("a,b,c,d,e");
    System.out.println(Arrays.toString(words));
}

prints

[a, b, c, d, e]

In the past, odd notations have been for "C" compatibility, but I wouldn't imagine someone writing this in C either.

Does anyone know why this is even allowed?

I am using Java 7 update 10, in case it matters.

This does the same thing in Java 6. http://ideone.com/91rZV1


BTW this doesn't compile, nor would I expect it to

public static <T> List mySplit(String s)<T> {
    return Collections.emptyList();
}

Java Solutions


Solution 1 - Java

> Does anyone know why this is even allowed?

In this case it's for backward compatibility with Java itself. From the JLS section 8.4:

> For compatibility with older versions of the Java SE platform, the declaration of a method that returns an array is allowed to place (some or all of) the empty bracket pairs that form the declaration of the array type after the formal parameter list. This is supported by the following obsolescent production, but should not be used in new code.

And yes, you should indeed regard it as an abomination which has no good purpose other than to shock other developers. Actually, you might be able to use it to win some money at parties by betting that it would compile, against folks who've never seen it...

Here's the kind of method which right-minded coders would expect to be invalid:

public String[] mwahahaha(String[] evil[])[] {
    return evil;
}

Solution 2 - Java

It's like

  String[] a; 

is the same as

  String a[];

Same works for the syntax of method return types

  public static String mySplit(String s)[] {

is the same as

  public static String[] mySplit(String s) {

But I think I never saw the version you mentioned in productive code yet.

Solution 3 - Java

I think its the same reason that the following variable declarations are both equivalent

String[] array
String array[]

this is a thing C developers do, so it was included to help them.

Solution 4 - Java

I believe it's just telling Java that the return type is an array of Strings, the same as declaring

static String[] mySplit(String s) {...

Similar to declaring variables:

String myStringArray[];

is equivalent to

String[] myStringArray;

Solution 5 - Java

Good question; when I implemented a Java parser I remember getting really confused by the JLS grammar at this point.

To expand on John's answer, here's what's going on:

  • this is called "mixed notation" in the spec
  • the grammar breaks type declarations into two pieces, each of which may have 0 or more []s

There are (at least) 5 places where this matters:

  • method type signatures
  • local variable declarations
  • field declarations
  • formal parameters
  • for-loops

Here's an excerpt from the grammar, focusing on method declarations:

MethodOrFieldDecl:
    Type Identifier MethodOrFieldRest

MethodOrFieldRest:  
    FieldDeclaratorsRest ;
    MethodDeclaratorRest

MethodDeclaratorRest:
    FormalParameters {[]} [throws QualifiedIdentifierList] (Block | ;)

Type:
    BasicType {[]}
    ReferenceType  {[]}

(Warning: it's difficult to read the grammar because the square and curly brackets are sometimes literals and sometimes metacharacters.)

This shows that [] can appear both under the Type rule, and as part of the MethodDeclaratorRest rule. It is optional in both places.

Solution 6 - Java

Yes, this is allowed,

same reason that:

String[] myArray;

is equivalent to

String myArray[];

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionPeter LawreyView Question on Stackoverflow
Solution 1 - JavaJon SkeetView Answer on Stackoverflow
Solution 2 - JavaMrSmith42View Answer on Stackoverflow
Solution 3 - JavaPeter ElliottView Answer on Stackoverflow
Solution 4 - JavaReinstate Monica -- notmaynardView Answer on Stackoverflow
Solution 5 - JavaMatt FenwickView Answer on Stackoverflow
Solution 6 - JavaAhmed KatoView Answer on Stackoverflow