Why java allows private String[][] var[]

后端 未结 5 2084
终归单人心
终归单人心 2021-01-20 03:21
private String[][] var[]

This code compiles. But shouldn\'t they make the array on type or variable?

5条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-20 04:02

    The Java Language Specification says

    "Brackets are allowed in declarators as a nod to the tradition of C and C++. The general rules for variable declaration, however, permit brackets to appear on both the type and in declarators, so that the local variable declaration"

    in JLS 10.2 . In my opinion square brackets ([]) in "types" make more sense of an array than that is in declared variables. But in order to confirm to the tradition of C/C++ it was done as the text says in JLS. Another thing is , mixing the square bracket with both types and variables would be confusing to the readers/programmers.

提交回复
热议问题