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?

4
  • 10
    woah, that's.. strange... ;D
    – Nanne
    Jan 15, 2011 at 11:35
  • 6
    see comments of this stackoverflow.com/questions/1995113/strangest-language-feature/…
    – josefx
    Jan 15, 2011 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, 2011 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, 2011 at 13:33

4 Answers 4

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.

2
  • 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, 2011 at 18:51
  • I have to say that I am laughing out loud since I saw that code! :-D
    – sdlins
    Dec 5, 2018 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...

2
  • 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, 2011 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 3, 2015 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.

2
  • is there an easier syntax for a function returning a pointer to an n dimensional array? (Not just an int*)
    – ted
    Mar 13, 2013 at 23:11
  • Actually, I must add, pcc supports directly f()[] meaning something like (*f()). Mar 28, 2017 at 14:43
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[]

2
  • 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, 2011 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, 2011 at 16:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.