125

I looked at some Java code today, and I found some weird syntax:

public class Sample {
  public int get()[] {
    return new int[]{1, 2, 3};
  }
}

I thought that can't compile and wanted to fix what I thought was a typo, but then I remembered the Java compiler did actually accept it!

Can someone please help me understand what it means? Is it an array of functions?

Macke
  • 24,812
  • 7
  • 82
  • 118
Johannes Schaub - litb
  • 496,577
  • 130
  • 894
  • 1,212
  • 10
    woah, that's.. strange... ;D – Nanne Jan 15 '11 at 11:35
  • 6
    see comments of this http://stackoverflow.com/questions/1995113/strangest-language-feature/1998146#1998146 – josefx Jan 15 '11 at 12:04
  • 2
    The Craziest ... Thing I've Ever Seen. They say ruby has a lot of magic, eat that ruby! – IAdapter Jan 15 '11 at 12:56
  • 16
    Doesn't even look remotely strange compared to a signature of a function which returns a pointer to array of pointers to functions in C. :) – Kos Jan 15 '11 at 13:33

4 Answers4

114

It's a method that returns an int[].

Java Language Specification (8.4 Method Declarations)

The declaration of a method that returns an array is allowed to place some or all of the bracket pairs that denote the array type after the formal parameter list. This syntax is supported for compatibility with early versions of the Java programming language. It is very strongly recommended that this syntax is not used in new code.

Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
Mehrdad Afshari
  • 414,610
  • 91
  • 852
  • 789
  • 95
    The best bit of this is that it's _some or all of_. So if you have a method returning a 3-dimensional array of ints, you can do: `public int[] foo()[][] {` and make people's eyes bleed. – Cowan Jan 15 '11 at 18:51
  • I have to say that I am *laughing out loud* since I saw that code! :-D – sdlins Dec 05 '18 at 15:49
22

That's a funny Question. In java you can say int[] a;, as well as int a[];.
From this perspective, in order to get the same result just need to move the []
and write public int[] get() {.
Still looks like the code came from an obfuscator...

Costis Aivalis
  • 13,680
  • 3
  • 46
  • 47
  • 2
    From an obfuscator? More likely from a C programmer (you can't return an array, but a pointer to array type - yes.. and it'd look similar). – Kos Jan 15 '11 at 13:32
  • 5
    @Kos: Why are you implying that an obfuscator and a C programmer must be two different things? Many times they are not! – Lii Jan 03 '15 at 21:32
12

As there is a C tag, I'll point out that a similar (but not identical) notation is possible in C and C++:

Here the function f returns a pointer to an array of 10 ints.

int tab[10];

int (*f())[10]
{
    return &tab;
}

Java simply doesn't need the star and parenthesis.

Simon Forsberg
  • 13,086
  • 10
  • 64
  • 108
AProgrammer
  • 51,233
  • 8
  • 91
  • 143
5

java's syntax allows for the following:

int[] intArr = new int[0];

and also

int intArr[] = new int[0];

which looks more fmiliar coming from the c-style syntax.

so too, with a function, the name can come before or after the [], and the type is still int[]

davin
  • 44,863
  • 9
  • 78
  • 78
  • 1
    This really annoys me as it really should be int[] because the fact it is an array is part of the type, not part of the name of it. The postgrad that writes my uni java courseworks does the opposite all the time in his code, its horrible! – danpalmer Jan 15 '11 at 15:52
  • 1
    @danpalmer, i agree, although put yourself in the shoes of a c programmer who inately has it in his blood to write int name[]. anyway, its not such a big deal, the language supports both and most java programmers use the int[] convention, so other than an annoying postgrad or two its rare. who knows, it might even work in your favour if they ask a question like this in your final – davin Jan 15 '11 at 16:12