Weird “[]” after Java method signature

后端 未结 4 1167
北海茫月
北海茫月 2020-12-23 08:48

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};
  }
}

4条回答
  •  醉梦人生
    2020-12-23 09:09

    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.

提交回复
热议问题