Java源码分析--java.lang.Integer

  • 将int转换为字符串(指定进制)
final static char[] digits = {
    '0' , '1' , '2' , '3' , '4' , '5' ,
    '6' , '7' , '8' , '9' , 'a' , 'b' ,
    'c' , 'd' , 'e' , 'f' , 'g' , 'h' ,
    'i' , 'j' , 'k' , 'l' , 'm' , 'n' ,
    'o' , 'p' , 'q' , 'r' , 's' , 't' ,
    'u' , 'v' , 'w' , 'x' , 'y' , 'z'
};
public static String toString(int i, int radix) {
    if (radix < Character.MIN_RADIX || radix > Character.MAX_RADIX)//只支持2-36进制
        radix = 10;
    /* Use the faster version */
    if (radix == 10) {
        return toString(i);
    }
    char buf[] = new char[33];//32位+符号位
    boolean negative = (i < 0);
    int charPos = 32;
    if (!negative) {
        i = -i;
    }
    while (i <= -radix) {//求每一位的值
        buf[charPos--] = digits[-(i % radix)];
        i = i / radix;
    }
    buf[charPos] = digits[-i];
    if (negative) {
        buf[--charPos] = '-';
    }
    return new String(buf, charPos, (33 - charPos));
}
  • 将int转换为无符号的n进制(n=[2, 8, 16],shift=4时,16进制)
private static String toUnsignedString(int i, int shift) {
    char[] buf = new char[32];
    int charPos = 32;
    int radix = 1 << shift;//转为redix进制, redix=16
    int mask = radix - 1;//mask = 15(1111)
    do {
        buf[--charPos] = digits[i & mask];
        i >>>= shift;//右移4位,处理下四位
    } while (i != 0);

    return new String(buf, charPos, (32 - charPos));
}
  • 将数字转换为String
final static char [] DigitTens = {
   '0', '0', '0', '0', '0', '0', '0', '0', '0', '0',
   '1', '1', '1', '1', '1', '1', '1', '1', '1', '1',
   '2', '2', '2', '2', '2', '2', '2', '2', '2', '2',
   '3', '3', '3', '3', '3', '3', '3', '3', '3', '3',
   '4', '4', '4', '4', '4', '4', '4', '4', '4', '4',
   '5', '5', '5', '5', '5', '5', '5', '5', '5', '5',
   '6', '6', '6', '6', '6', '6', '6', '6', '6', '6',
   '7', '7', '7', '7', '7', '7', '7', '7', '7', '7',
   '8', '8', '8', '8', '8', '8', '8', '8', '8', '8',
   '9', '9', '9', '9', '9', '9', '9', '9', '9', '9',
   } ;

final static char [] DigitOnes = {
   '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
   '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
   '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
   '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
   '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
   '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
   '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
   '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
   '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
   '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
   } ;
public static String toString(int i) {
    if (i == Integer.MIN_VALUE)
        return "-2147483648";
    int size = (i < 0) ? stringSize(-i) + 1 : stringSize(i);
    char[] buf = new char[size];
    getChars(i, size, buf);
    return new String(buf, true);
}
static void getChars(int i, int index, char[] buf) {
    int q, r;
    int charPos = index;
    char sign = 0;

    if (i < 0) {
        sign = '-';
        i = -i;
    }

    // Generate two digits per iteration
    while (i >= 65536) {
        q = i / 100; 
    // really: r = i - (q * (2^6+2^5+2^2)); (2^6+2^5+2^2=100)
        r = i - ((q << 6) + (q << 5) + (q << 2));//r是个位与十位的两位数  r = 10011 - (10011 / 100)*100 = 11
        i = q;
        buf [--charPos] = DigitOnes[r];
        buf [--charPos] = DigitTens[r];
    }
    //这里大于65536时用除法,而且一次取两位,因为除法运算慢
    //为什么选择65536(2^16)?为了防止下面的 i*52429 溢出
    // Fall thru to fast mode for smaller numbers
    // assert(i <= 65536, i);
    for (;;) {
        q = (i * 52429) >>> (16+3);//(i*52429)/2^19=(i*52429)/524288≈i/10
        r = i - ((q << 3) + (q << 1));  // r = i-(q*10)
        buf [--charPos] = digits [r];
        i = q;
        if (i == 0) break;
    }
    if (sign != 0) {
        buf [--charPos] = sign;
    }
}
  • 将其它类型转换为Integer(-128-127的数不在创建新对象,而是从缓存中拿)
public static Integer valueOf(int i) {
    assert IntegerCache.high >= 127;
    if (i >= IntegerCache.low && i <= IntegerCache.high)
        return IntegerCache.cache[i + (-IntegerCache.low)];
    return new Integer(i);
}
  • equals比较方法
public boolean equals(Object obj) {
    if (obj instanceof Integer) {
        return value == ((Integer)obj).intValue();
    }
    return false;
}
--------------------------------------------------------------
// 自动装箱:将基本数据类型转换为包装类
Integer number1 = 10;
// 自动拆箱:将包装类转换为基本数据类型
int number2 = new Integer(10);
Sysout.out.print(number1 == number2);// true, number1被自动拆箱了

猜你喜欢

转载自blog.csdn.net/weixin_43210354/article/details/82713154