jdk8源码学习之Integer

Integer类

public final class Integer extends Number implements Comparable<Integer>{}

Integer是用final声明的常量类,不能被任何类所继承。并且Integer类继承了Number类和实现了Comparable接口。Number类是一个抽象类,8中基本数据类型的包装类除了Character和Boolean没有继承该类外,剩下的都继承了Number类,该类的方法用于各种数据类型的转换。Comparable接口就一个compareTo方法,用于元素之间的大小比较,下面会对这些方法详细展开介绍。

构造方法

public Integer(int value) {
        this.value = value;
    }


public Integer(String s) throws NumberFormatException {
        this.value = parseInt(s, 10);
    }

toString()  toString(int i)   toString(int i, int radix)

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

        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));
    }

parseInt()

public static int parseInt(String s, int radix)
            throws NumberFormatException
{
    /*
     * WARNING: This method may be invoked early during VM initialization
     * before IntegerCache is initialized. Care must be taken to not use
     * the valueOf method.
     */

    if (s == null) {    // 如果接受的字符串为空, 就报空字符串的异常
        throw new NumberFormatException("null");
    }

    if (radix < Character.MIN_RADIX) {      // 判断基数是不是符合要求
        throw new NumberFormatException("radix " + radix +
                                        " less than Character.MIN_RADIX");
    }

    if (radix > Character.MAX_RADIX) {  // 判断基数是不是符合要求
        throw new NumberFormatException("radix " + radix +
                                        " greater than Character.MAX_RADIX");
    }

    int result = 0;
    boolean negative = false;       // 判断符号
    int i = 0, len = s.length();    // 设置初始位置和字符串的长度
    int limit = -Integer.MAX_VALUE; 
    int multmin;
    int digit;

    if (len > 0) {      // 字符串的长度必须大于零
        char firstChar = s.charAt(0);   // 获得字符串的第一个字符
        if (firstChar < '0') { // Possible leading "+" or "-"
            if (firstChar == '-') {
                negative = true;
                limit = Integer.MIN_VALUE;
            } else if (firstChar != '+') // 如果不为++的话就报错
                throw NumberFormatException.forInputString(s);
            // 字符串的长度为1但是又不是数字, 那肯定就出错了
            if (len == 1) // Cannot have lone "+" or "-" 
                throw NumberFormatException.forInputString(s);
            i++;
        }
        multmin = limit / radix;

        /*
         * 下面的过程其实很好理解, 以8进制的"534"为例
         * (-5*8-3)*8-4 = -348, 根据符号位判断返回的是348
         */

        while (i < len) {
            // Accumulating negatively avoids surprises near MAX_VALUE

            // 除了前面的判断这里的也有点复杂, 因为要考虑到各种进位
            // 这个将i位置上的字符根据基数转为实际的值, A->11
            digit = Character.digit(s.charAt(i++),radix);
            if (digit < 0) {
                throw NumberFormatException.forInputString(s);
            }
            if (result < multmin) {
                throw NumberFormatException.forInputString(s);
            }
            result *= radix;
            if (result < limit + digit) {
                throw NumberFormatException.forInputString(s);
            }
            result -= digit;
        }
    } else {
        throw NumberFormatException.forInputString(s);
    }
    return negative ? result : -result;     // 根据符号位来判断返回哪一个
}
  1. 没想通的一点是,常理来说(至少我是这样的哈)是考虑用加法,然后再根据符号位判断正负,但是源码中用的是减法。这点没想通是为什么,虽然也没差,感觉怪怪的。

  2. digit = Character.digit(s.charAt(i++),radix);
    这里的函数调用里面的代码也挺多的。根据该位上的字符和基数来得到对应的数字。

valueOf(int i)

  public static Integer valueOf(int i) {
        if (i >= IntegerCache.low && i <= IntegerCache.high)
            return IntegerCache.cache[i + (-IntegerCache.low)];
        return new Integer(i);
    }

可以看到这里使用到了IntegerCache缓存,IntegerCache默认缓存-128~127之间的Integer。IntegerCache是Integer类的静态内部类。

private static class IntegerCache {
    static final int low = -128; //默认low=-128
    static final int high; //high可以配置,通过 VM 参数-XX:AutoBoxCacheMax=<size>
        //high可以配置,所以默认缓存-128~127,但是也可以缓存另外的常用数。
    static final Integer cache[]; //缓存数组

    //静态代码块,Integer类加载时就缓存。
    static {
        // high value may be configured by property
        int h = 127; //默认127
        String integerCacheHighPropValue =
            sun.misc.VM.getSavedProperty("java.lang.Integer.IntegerCache.high"); //读取VM参数配置。
        if (integerCacheHighPropValue != null) {
            try {
                int i = parseInt(integerCacheHighPropValue);
                i = Math.max(i, 127); //缓存大数
                // Maximum array size is Integer.MAX_VALUE
                h = Math.min(i, Integer.MAX_VALUE - (-low) -1); //防止越界
            } catch( NumberFormatException nfe) {
                // If the property cannot be parsed into an int, ignore it.
            }
        }
        high = h;

        cache = new Integer[(high - low) + 1]; //创建缓存数组。
        int j = low;
        for(int k = 0; k < cache.length; k++)
            cache[k] = new Integer(j++); //缓存。

        // range [-128, 127] must be interned (JLS7 5.1.7)
        assert IntegerCache.high >= 127; //保证[-128, 127]在缓存范围内。
    }

    private IntegerCache() {}
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值