In last week’s article, we looked at various ways to concatenate strings. In the Append method of StringBuilder and StrngBuffer’s abstract parent class AbstractStringBuilder, we used the ensureCapacityInternal function. So today I’m going to show you a little bit about this function

Link to previous issue:Java – Multiple ways to concatenate strings (1)

// Call the function, Private void ensureCapacityInternal(int minimumCapacity) {// overflow-conscious code if (minimumCapacity) -value. Length > 0) {value = arrays.copyof (value, newCapacity(minimumCapacity)); }}
// return newCapacity private int newCapacity(int minCapacity) {// overflow-conscious code // Int newCapacity = (value. Length << 1) + 2; int newCapacity = (value. Length << 1) + 2; If (newCapacity - minCapacity < 0) {newCapacity = minCapacity; } // For example, when the value. Length is 2136372536, the sign bit of newCapacity changes due to overflow. // The sign bit of newCapacity exceeds the maximum value of int, so the sign bit becomes "1". When return < = 0 condition (newCapacity < = 0 | | MAX_ARRAY_SIZE - newCapacity < 0)? hugeCapacity(minCapacity) : newCapacity; }
// For the acquisition of huge new capacity // Personal understanding: the length of data and objects is not really determined by MAX_ARRAY_SIZE, but by the storage of the JVM virtual machine that you are actually storing // Case 1: For example, if your int array is 2.1 billion in length, it needs 2.1 billion by 4 bytes, which is estimated to be 8.4g. This is usually more than the size of the JVM heap we have set. // This is why when we create an array in superheap memory, it will issue OutOfMemoryError: Java heap space // Case 2: When we create data in the hyperJVM specification, such as integer.max_value -1 and integer.max_value; OutOfMemoryError: Requested array size exceeds VM limit; Perhaps different JVMS have different bottom lines // Case 3: MAX_VALUE, the value passed to StringBuilder exceeds the maximum value of int, and the underlying value becomes negative. He will quote us NegativeArraySizeException private int hugeCapacity (int minCapacity) {if (Integer. MAX_VALUE - minCapacity < 0) {/ /  overflow throw new OutOfMemoryError(); } return (minCapacity > MAX_ARRAY_SIZE)? minCapacity : MAX_ARRAY_SIZE; }

conclusion

1. Personal doubts: In the process of learning the source code, I was thinking why MAX_ARRAY_SIZE=Integer.MAX_VALUE – 8, the query said to store some key information? Why can hugeCapacity in arrayList exceed MAX_ARRAY_SIZE? Why can you exceed MAX_ARRAY_SIZE, but I can’t?

2. Small talk: When I was writing code, I looked up a lot of information. Sometimes I wondered why I was writing a blog. More broadly, I want to see cool code and see what great programmers have done. Went to small said, I’ve hope one day when I leave this world, there are book it records I since the childhood life experience and comprehension, writing a blog is also a kind of record, maybe one day, when I saw these blogs will recall those night typing time, perhaps it is also a kind of young memories, ha ha ha!