java - Why does AbstractStringBuilder.append behave differently for MIN_VALUE? -
consider following methods in java.lang.abstractstringbuilder
long
public abstractstringbuilder append(long l) { if (l == long.min_value) { append("-9223372036854775808"); return this; } int appendedlength = (l < 0) ? long.stringsize(-l) + 1 : long.stringsize(l); int spaceneeded = count + appendedlength; ensurecapacityinternal(spaceneeded); long.getchars(l, spaceneeded, value); count = spaceneeded; return this; } integer
public abstractstringbuilder append(int i) { if (i == integer.min_value) { append("-2147483648"); return this; } int appendedlength = (i < 0) ? integer.stringsize(-i) + 1 : integer.stringsize(i); int spaceneeded = count + appendedlength; ensurecapacityinternal(spaceneeded); integer.getchars(i, spaceneeded, value); count = spaceneeded; return this; } why abstractstringbuilder#append use different algorithm append min_value ?
because integer.stringsize requires non-negative argument. code looks this:
final static int [] sizetable = { 9, 99, 999, 9999, 99999, 999999, 9999999, 99999999, 999999999, integer.max_value }; // requires positive x static int stringsize(int x) { (int i=0; ; i++) if (x <= sizetable[i]) return i+1; }
Comments
Post a Comment