Duplicate :
|
|
Relates :
|
|
Relates :
|
java.lang.StringBuilder.ensureCapacity(int minCap) method throws OutOfMemoryError Exception with the minCap value near to the Integer.MIN_VALUE. See the following simple code: public class TestEnsureCapacity { public static void main(String[] args) { StringBuilder sb = new StringBuffer("abc"); int cap = sb.capacity(); sb.ensureCapacity(Integer.MIN_VALUE); } } The Exception message will be thrown: Exception in thread "main" java.lang.OutOfMemoryError at java.lang.AbstractStringBuilder.expandCapacity(AbstractStringBuilder.java:125) at java.lang.AbstractStringBuilder.ensureCapacityInternal(AbstractStringBuilder.java:112) at java.lang.AbstractStringBuilder.ensureCapacity(AbstractStringBuilder.java:103) at java.lang.StringBuilder.ensureCapacity(StringBuilder.java:72) at TestEnsureCapacity.main(TestEnsureCapacity.java:6) In that time JDK specification say nothing about possible exceptions for the java.lang.StringBuilder.ensureCapacity method: If the minimumCapacity argument is nonpositive, this method takes no action and simply returns. This is regression since JDK 7 b94.
|