Java之Integer

一、方法思维导图

在这里插入图片描述
Integer主要包含: 类型转换、二进制数操作。

二、部分方法源码

valueOf(int i)
// jdk 1.7

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

Integer使用了缓存, 当数值在某个范围时, 直接返回已经缓存的好的对象。缓存的默认范围是[-128, 127] 缓存的最大值可以通过JVM 的启动参数 -XX:AutoBoxCacheMax=size 修改。

// jdk 1.7

private static class IntegerCache {
        static final int low = -128;
        static final int high;
        static final Integer cache[];

        static {
            // high value may be configured by property
            int h = 127;
            String integerCacheHighPropValue =
                sun.misc.VM.getSavedProperty("java.lang.Integer.IntegerCache.high");
            if (integerCacheHighPropValue != null) {
                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);
            }
            high = h;

            cache = new Integer[(high - low) + 1];
            int j = low;
            for(int k = 0; k < cache.length; k++)
                cache[k] = new Integer(j++);
        }

        private IntegerCache() {}
    }

parseInt(String s)
public static int parseInt(String s) throws NumberFormatException {
        return parseInt(s,10);
    }

默认状态下转换为10进制

// radix表示进制

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

                if (len == 1) // Cannot have lone "+" or "-"
                    throw NumberFormatException.forInputString(s);
                i++;
            }
            multmin = limit / radix;
            while (i < len) {
                // Accumulating negatively avoids surprises near MAX_VALUE
                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.判断一个字符是否是数字
调用Character.digit(char ch, int radix) 进行判断。

2.判断溢出

情况一:

 if (result < multmin) {
  throw NumberFormatException.forInputString(s);
}

这个判断主要是用于当参数的位数大于限定值时, 并且参数的前(n-1)位与限定值相同。譬如: 当限定值为11(2位数), 传入参数为110(3位数)


情况二:

// digit表示读出的字符代表的数字

 if (result < limit + digit) {
   throw NumberFormatException.forInputString(s);
}

假如某种类型的数的最大值为100, 传入的字符串为 “101”。通常的做法是将每一个字符读出来, 转换成数字然后相加, 但是当进行 “100+1” 时数字就会溢出了, 无法进行正确的比较。

它的思路是既然使用 a + b > limit不能判断 ,那么转换成 -a < -limit + b进行判断, 这样就不会有溢出的问题, 这就是为什么limit为负数的原因。

对于int类型正数来说, -Integer.MAX_VALUE大于Integer.MIN_VALUE, 它还在Integer的范围内。即使与某数相加也不会溢出。

对于负数来说, Integer.MIN_VALUE + b也没有溢出。

参考资料:
JDK 1.7 Integer.parseInt 源码解析

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值