看了java 1.7.0_09版本的源码,发现Integer类的toString方法的实现使用负int来计算mod运算,有什么意义吗?代码如下:
public static String toString(int i, int radix) {
if (radix < Character.MIN_RADIX || radix > Character.MAX_RADIX)
radix = 10;
/* Use the faster version */
if (radix == 10) {
return toString(i);
}
char buf[] = new char[33];
boolean negative = (i < 0);
int charPos = 32;
if (!negative) {
i = -i; //***** change i to negative
}
while (i <= -radix) {
buf[charPos--] = digits[-(i % radix)]; //***** change back to positive after
//***** mod operation
i = i / radix;
}
buf[charPos] = digits[-i];
if (negative) {
buf[--charPos] = '-';
}
return new String(buf, charPos, (33 - charPos));
}