Why the maximum array size of ArrayList is Integer.MAX_VALUE - 8?

前端 未结 3 2035
一生所求
一生所求 2020-12-06 10:22

I am studying Java 8 documentation for ArrayList. I got that maximum array size is defined as Integer.MAX_VALUE - 8 means 2^31 – 8 = 2 147

3条回答
  •  借酒劲吻你
    2020-12-06 10:44

    The value is a worst-case scenario. Note the comment:

    Attempts to allocate larger arrays may result in OutOfMemoryError

    It doesn't say will, just may. If you stay below this value, you should have no issue (as long as memory is available, of course).

    You may want to look at the answers to this question for more information:
    Why I can't create an array with large size?

提交回复
热议问题