In Java, why are arrays objects? Are there any specific reasons?

前端 未结 6 2268
半阙折子戏
半阙折子戏 2020-12-17 23:37

Is there any reason why an array in Java is an object?

6条回答
  •  情歌与酒
    2020-12-18 00:33

    Another point is that objects are mutable and are passed by reference. In arrays there aren't any fields/methods that you can use to change "properties" of the array, but you sure can mutate the element values. And the benefits of passing arrays by reference are pretty obvious (though functional programmers probably wish Java had immutable lists passed by value).

    Edit: forgot to mention. In the period before autoboxing, it was helpful to be able to store arrays in collections, write them to ObjectStreams etc.

提交回复
热议问题