This is the lastest JDK version (2022 June), JDK 18, and you can download it on https://jdk.java.net/18/
or we can look the source on Github
https://github.com/openjdk/jdk/blob/master/src/java.base/share/classes/java/util/ArrayList.java
When you add an item into your ArrayList, Java will ensure that it can hold at least the items was in it and the new item. Java prefer to increase the capacity to 1.5* current capacity according to this expression
int newCapacity = ArraysSupport.newLength(oldCapacity,
minCapacity - oldCapacity, /* minimum growth */
oldCapacity >> 1 /* preferred growth */);.
oldCapacity >> 1
means 0.5 * oldCapacity
so newLength will be 1.5 * oldCapacity if it is positive and not overflow
Here are the code snippets:
src/java.base/share/classes/java/util/ArrayList.java
public boolean add(E e) {
modCount++;
add(e, elementData, size);
return true;
}
private void add(E e, Object[] elementData, int s) {
if (s == elementData.length)
elementData = grow();
elementData[s] = e;
size = s + 1;
}
private Object[] grow() {
return grow(size + 1);
}
private Object[] grow(int minCapacity) {
int oldCapacity = elementData.length;
if (oldCapacity > 0 || elementData != DEFAULTCAPACITY_EMPTY_ELEMENTDATA) {
int newCapacity = ArraysSupport.newLength(oldCapacity,
minCapacity - oldCapacity, /* minimum growth */
oldCapacity >> 1 /* preferred growth */);
return elementData = Arrays.copyOf(elementData, newCapacity);
} else {
return elementData = new Object[Math.max(DEFAULT_CAPACITY, minCapacity)];
}
}
src/java.base/share/classes/jdk/internal/util/ArraysSupport.java
public static int newLength(int oldLength, int minGrowth, int prefGrowth) {
int prefLength = oldLength + Math.max(minGrowth, prefGrowth); // might overflow
if (0 < prefLength && prefLength <= SOFT_MAX_ARRAY_LENGTH) {
return prefLength;
} else {
return hugeLength(oldLength, minGrowth);
}
}