java-string转换成integer的方式及原理

1 Integer.parseInt(String str)方法

public static int parseInt(String s) throws NumberFormatException {
        //内部默认调用parseInt(String s, int radix)基数设置为10
        return parseInt(s,10);
    }

2 Integer.parseInt(String s, int 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.
         */
        //判断字符是否为null
        if (s == null) {
            throw new NumberFormatException("s == 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;
        //char字符数组下标和长度
        int i = 0, len = s.length();
        //限制
        int limit = -Integer.MAX_VALUE;
        int multmin;
        int digit;
        //判断字符长度是否大于0,否则抛出异常
        if (len > 0) {
            //第一个字符是否是符号
            char firstChar = s.charAt(0);
            //根据ascii码表看出加号(43)和负号(45)对应的
            //十进制数小于‘0’(48)的
            if (firstChar < '0') { // Possible leading "+" or "-"
                //是负号
                if (firstChar == '-') {
                    //负号属性设置为true
                    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;
            while (i < len) {
                // Accumulating negatively avoids surprises near MAX_VALUE
                //返回指定基数中字符表示的数值。(此处是十进制数值)
                digit = Character.digit(s.charAt(i++),radix);
                //小于0,则为非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;
    }

3 Character.digit(char ch, int radix)方法

返回指定基数中字符表示的数值。

public static int digit(int codePoint, int radix) {
        //基数必须再最大和最小基数之间
        if (radix < MIN_RADIX || radix > MAX_RADIX) {
            return -1;
        }
        
        if (codePoint < 128) {
            // Optimized for ASCII
            int result = -1;
            //字符在0-9字符之间
            if ('0' <= codePoint && codePoint <= '9') {
                result = codePoint - '0';
            }
            //字符在a-z之间
            else if ('a' <= codePoint && codePoint <= 'z') {
                result = 10 + (codePoint - 'a');
            }
            //字符在A-Z之间
            else if ('A' <= codePoint && codePoint <= 'Z') {
                result = 10 + (codePoint - 'A');
            }
            //通过判断result和基数大小,输出对应值
            //通过我们parseInt对应的基数值为10,
            //所以,只能在第一个判断(字符在0-9字符之间)
            //中得到result值 否则后续程序会抛出异常
            return result < radix ? result : -1;
        }
        return digitImpl(codePoint, radix);
    }

4 总结

  1. parseInt(String s)--内部调用parseInt(s,10)(默认为10进制)
  2. 正常判断null,进制范围,length等
  3. 判断第一个字符是否是符号位
  4. 循环遍历确定每个字符的十进制值
  5. 通过*= 和-= 进行计算拼接
  6. 判断是否为负值 返回结果。

参考

string转换成integer的方式及原理

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 前言 最先接触编程的知识是在大学里面,大学里面学了一些基础的知识,c语言,java语言,单片机的汇编语言等;大学毕...
    oceanfive阅读 3,151评论 0 7
  • (一) java基础面试知识点 1.java中==和equals和hashCode的区别 答:首先回答的是三个定义...
    一生有你_b4f6阅读 582评论 1 1
  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 31,779评论 18 399
  • 读书的方法有很多,我最为推崇的,也是我自己一直使用的方法,是从李笑来老师专栏磨炼学习到的只字不差的阅读方式。 只字...
    感想心得阅读 447评论 0 1
  • 听说你很是迷恋人世 总把自己补满人家的墙壁 偷偷的借着皎洁的月光 女孩在角落里偷偷的哭泣 看惯了世间的风风雨雨 触...
    垄上行云阅读 353评论 2 1