Java’s strange bit of syntax
Tags: java, syntax, array
Published: 2020-12-26 00:35:21

Have you ever seen code like this ?

```java
public int getLootBoxContents(int lootBoxIdx)[] {
    // TODO: get loot
    return null;
}
```

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 returning `null` on an `int`, so the return type should be `int[]` 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

```java
public int[] getLootBoxContents(int lootBoxIdx)
```

and not

```java
public int getLootBoxContents(int lootBoxIdx)[]
```

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:

```log
C-style array declaration of the return type of method 'lootBoxContents()' 
```

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:

```c
int* getLootBoxContents(int lootBoxIdx)
```

and if we want to return a fixed size array, we use

```c
int (&getLootBoxContents(int lootBoxIdx))[5] // each loot box must be an array of size 5
```

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.

```java
int[] array;
int array[];
```

The declarations on the two rows above are equivalent according to the [java specification](https://docs.oracle.com/javase/specs/jls/se15/html/jls-10.html#jls-10.2).

```log
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.
```

Or put simply, even the language writers advice us to always prefer `int[] array`.

Now the interesting bit from the same specification is:

```log
Brackets are allowed in declarators as a nod to the tradition of C and C++.
```

**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.