private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;
private void grow(int minCapacity) {
// overflow-conscious code
int oldCapacity = elementData.length;
int newCapacity = oldCapacity + (oldCapacity >> 1);
if (newCapacity - minCapacity < 0)
newCapacity = minCapacity;
if (newCapacity - MAX_ARRAY_SIZE > 0)
newCapacity = hugeCapacity(minCapacity);
// minCapacity is usually close to size, so this is a win:
elementData = Arrays.copyOf(elementData, newCapacity);
}
private static int hugeCapacity(int minCapacity) {
if (minCapacity < 0) // overflow
throw new OutOfMemoryError();
return (minCapacity > MAX_ARRAY_SIZE) ?
Integer.MAX_VALUE :
MAX_ARRAY_SIZE;
}
< hr >
the above specifies that the size of MAX_ARRAY_SIZE is the judgment
if (newCapacity-MAX_ARRAY_SIZE > 0 in the Integer.MAX_VALUE-8 if (newCapacity function, indicating that if newCapacity is greater than MAX_ARRAY_SIZE, the hugeCapacity () function is executed and (minCapacity > MAX_ARRAY_SIZE)? Integer.MAX_VALUE: MAX_ARRAY_SIZE; is returned I don"t understand here. MAX_ARRAY_SIZE equals Integer.MAX_VALUE-8. Aren"t the 8 bytes in memory to store object header information and object header information? Why can you create an array of Integer.MAX_VALUE size if you exceed MAX_ARRAY_SIZE? is this 8-byte space optional? Don"t they contradict each other? What exactly is the function hugeCapacity () judging?