1>compareTo(Integer anotherInteger) : 在数字上比较两个 Integer 对象。
返回有三个结果:
:相等返回0
:first > second 返回1
:first < second 返回-1
源码如下:很简单不多说了
public int compareTo(Integer anotherInteger) {
int thisVal = this.value;
int anotherVal = anotherInteger.value;
return (thisVal<anotherVal ? -1 : (thisVal==anotherVal ? 0 : 1));
}
**2>decode(String nm) :
将 String 解码为 Integer。**
看着看着看晕了 ,先放这吧,以后再看
**3>hashCode() :
返回此 Integer 的哈希码。**
直接返回数值作为hashcod public int hashCode() {
return value;
}
**4>toString()
返回一个表示该 Integer 值的 String 对象。**
其实这个方法建议大家debug模式下找找源码,其实真正实现转换的代码如下
/**
*
* @param i 要转换的值,这里是533
* @param index 分配的大小值,因为是3位数,所以这里给分配了3
* @param buf 生成了一个大小为index的数组
*/
static void getChars(int i, int index, char[] buf) {
int q, r;
int charPos = index; //1000
char sign = 0;
if (i < 0) {
sign = '-';
i = -i;
}
// Generate two digits per iteration
while (i >= 65536) {
q = i / 100;
// really: r = i - (q * 100);
r = i - ((q << 6) + (q << 5) + (q << 2));
i = q;
buf [--charPos] = DigitOnes[r];
buf [--charPos] = DigitTens[r];
}
// Fall thru to fast mode for smaller numbers
// assert(i <= 65536, i);
/**
这里是一般情况
*/
for (;;) {
q = (i * 52429) >>> (16+3);//相当于取余 533 /10 ==>> 53 第二次 53/10 ==>> 5 三次 为0
r = i - ((q << 3) + (q << 1)); // r = i-(q*10) 3 3 5
buf [--charPos] = digits [r];//倒着采用查表法给buf数组填数
i = q;
if (i == 0) break;
}
if (sign != 0) {
buf [--charPos] = sign;
}
}
“`
这个方法关联的源码太多,建议大家自己认真瞅瞅