Strange array return type
Solution 1:
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:
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:
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:
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;