美文网首页
Integer源码分析

Integer源码分析

作者: 心中翼 | 来源:发表于2019-03-11 11:41 被阅读0次

toString()

public String toString() {
        return toString(value);
    }
    final static int [] sizeTable = { 9, 99, 999, 9999, 99999, 999999, 9999999,
                                      99999999, 999999999, Integer.MAX_VALUE };

    public static String toString(int i) {
        //1
        if (i == Integer.MIN_VALUE)
            return "-2147483648";
        //2
        int size = (i < 0) ? stringSize(-i) + 1 : stringSize(i);
       //3
        char[] buf = new char[size];
       //4
        getChars(i, size, buf);
        return new String(buf, true);
    }

    static int stringSize(int x) {
        for (int i=0; ; i++)
            if (x <= sizeTable[i])
                return i+1;
    }

1、如果Integer的value值正好是 Integer.MIN_VALUE 直接返回 “-2147483648” 节省时间。
2、得到integer值的十进制的长度,如果负数先求出绝对值的长度,然后再长度加1,因为负数的符号位占一位。 stringSize 非常的巧妙的来获取整数的长度,根据sizeTable数组的值区间来确定,字符串长度。
3、根据十进制的长度,声明一个char数组。
4、填充char数组

    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  每次迭代生成两位数字
       // 1
        while (i >= 65536) {
         //除以100求得商q
            q = i / 100;
        // really: r = i - (q * 100);
           //求得余数r
            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);
       //2
        for (;;) {
           //i除以10的商 q
            q = (i * 52429) >>> (16+3);
          //i 减去商乘以10,得余数r
            r = i - ((q << 3) + (q << 1));  // r = i-(q*10) ...
         //buf填充字符
            buf [--charPos] = digits [r];
            i = q;
            if (i == 0) break;
        }
        if (sign != 0) {
            buf [--charPos] = sign;
        }
    }

    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',
        } ;

1、分成两部分来计算,大于65536部分,每次除以100的余数
利用DigitOnes,DigitTens两个数组获取个位数及十位数字符
2、小于65536部分,除10求余,根据余数获取对应的字符。

相关文章

  • java.lang.Integer#parseInt() 源码分

    java.lang.Integer#parseInt() 源码分析 Integer#parseInt() 是我们经...

  • 享元模式源码分析

    JDK源码分析 Integer Integer的缓存默认范围是:-128~127 Integer可设置最大值:12...

  • Integer源码分析

    title: Integer源码分析date: 2017-09-11 15:07:46tags: javacate...

  • Integer源码分析

    Integer 类的常量 MIN_VALUE : -231 , int 类型能够表示的最小值 MAX_VALUE ...

  • Integer源码分析

    toString() 1、如果Integer的value值正好是 Integer.MIN_VALUE 直接返回 “...

  • Integer源码分析

    Integer Integer是int的包装类而int是一种基本数据类型。 Integer是面向对象的,所以必须实...

  • JDK源码分析 Integer

    说明 对于JDK源码分析的文章,仅仅记录我认为重要的地方。源码的细节实在太多,不可能面面俱到地写清每个逻辑。所以我...

  • java Integer 源码分析

    Interger是int的包装类型,实现了Number接口,具有可比较和可序列化特性: Integer可以表示的范...

  • 2018-04-24常用类

    常用类_包装类_Integer_Number_JDK源码分析 把一个数zuoweiobject处理

  • java基础:Integer — 源码分析

    其他更多java基础文章:java基础学习(目录) 转载自 Java 源码学习系列(三)——Integer学习的过...

网友评论

      本文标题:Integer源码分析

      本文链接:https://www.haomeiwen.com/subject/vbuxpqtx.html