Weird “[]” after Java method signature

后端 未结 4 1185
北海茫月
北海茫月 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:26

    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[]

提交回复
热议问题