Have you ever seen code like this ?
public int getLootBoxContents(int lootBoxIdx)[] {
// TODO: get loot
return null;
}
Enter fullscreen mode Exit fullscreen mode
If you do not look carefully you might actually miss it
“Wait, what are those
[]
doing at the end of the method declaration? You can not be returningnull
on anint
, so the return type should beint[]
right?”
Yeap, you are correct and I really hope you have not seen this syntax in production code. Now apart from this being a peace of trivia knowledge you can use to impress your equally as nerdy friends at the bar, the real question is why is this allowed? Everywhere we look, in every guideline we see, we would declare the method signature as
public int[] getLootBoxContents(int lootBoxIdx)
Enter fullscreen mode Exit fullscreen mode
and not
public int getLootBoxContents(int lootBoxIdx)[]
Enter fullscreen mode Exit fullscreen mode
Syntactically similar languages like C# don’t allow it as well, so what gives?
If you see this code in the always handy IntelliJ it will give you a hint on it’s origin:
C-style array declaration of the return type of method 'lootBoxContents()'
Enter fullscreen mode Exit fullscreen mode
For those of you who have not written C code, in C (C++ as well) arrays are not first class citizens in the language, which means they can not be used as the return type of a function. What we do in C instead is return a pointer to the array like so:
int* getLootBoxContents(int lootBoxIdx)
Enter fullscreen mode Exit fullscreen mode
and if we want to return a fixed size array, we use
int (&getLootBoxContents(int lootBoxIdx))[5] // each loot box must be an array of size 5
Enter fullscreen mode Exit fullscreen mode
OK, the second one looks a lot similar to what we have and we know that Java has inter-op with C with the magical native
keyword. Still this is pure Java, not some Java to C shenanigans, so why is the syntax allowed?
Let’s look at another example, that is more familiar to developers.
int[] array;
int array[];
Enter fullscreen mode Exit fullscreen mode
The declarations on the two rows above are equivalent according to the java specification.
The precise array type depends on the bracket pairs that may appear as part of the type at the beginning of the method declaration, or after the method's formal parameter list, or both.
...
We do not recommend "mixed notation" in array variable declarations, where bracket pairs appear on both the type and in declarators; nor in method declarations, where bracket pairs appear both before and after the formal parameter list.
Enter fullscreen mode Exit fullscreen mode
Or put simply, even the language writers advice us to always prefer int[] array
.
Now the interesting bit from the same specification is:
Brackets are allowed in declarators as a nod to the tradition of C and C++.
Enter fullscreen mode Exit fullscreen mode
And here we have our answer and it’s not simply a nod, as stated. If we go back in time to Java’s inception the dominant languages at that time were C and C++, so this syntax feature was also added to the language to help C/C++ developers in their transition to writing Java. Did it actually help with that … highly unlikely, but still it’s part of the language that will never go away and a nice peace of programing language history.
暂无评论内容