Java源码随笔--Array

/ Java / 0 条评论 / 1990浏览

Array最大长度为 最大Integer长度-8 即:

@Native public static final int MAX_VALUE = 0x7fffffff;

 /**
     * The maximum size of array to allocate.
     * Some VMs reserve some header words in an array.
     * Attempts to allocate larger arrays may result in
     * OutOfMemoryError: Requested array size exceeds VM limit
     */
    private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;

原因是一些Vms会保存头关键字在数组中,预留8位 防止溢出.如果试图定义更大长度的数组会导致OutOfMemoryError.


多数操作会用到复制列表的方法 最根本的复制操作是:

System.arraycopy(elementData, 0, a, 0, size);