zoukankan      html  css  js  c++  java
  • 【夯实基础】- Integer.valueof()和Integer.parseInt()的区别

      今天在看公司代码的时候,看到有人在将 String 转为 int 时,用到了Integer.parseInt(String s)方法,我一直用的是Integer.valueOf(String s)方法,parseInt()方法之前只是在JavaScript中看到过,有点感兴趣,就去看了一下他们的源码。

      发现 Integer.valueof()Integer.parseInt() 的底层都是用的Integer.parseInt(String s ,int radix)这个方法。在这里给这个方法做一下解释。

      Integer.parseInt(String s ,int radix),radix用来表示传进来的值是什么进制的,并返回10进制的 int 类型的结果 

      比如Integer.parseInt(“A”,16),则输出结果为10进制的10,其中16表示"A"是一个16进制的值。

      根据:Character.MIN_RADIX=2和Character.MAX_RADIX=36 则,parseInt(String s, int radix)参数中radix的范围是在2~36之间,超出范围会抛异常。其中s的长度也不能超出7,否则也会抛异常。其中限制在36位之内是因为数字加字母刚好可以表示到36位,比如Integer.parseInt(“Z”,36),输出结果为35。

      以下为parseInt(String s ,Int radix)的源代码实现。

    public static int parseInt(String s, int radix) throws NumberFormatException  {  
        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, max = s.length();  
        int limit;  
        int multmin;  
        int digit;  
    
        if (max > 0) {  
            if (s.charAt(0) == '-') {  
                negative = true;  
                limit = Integer.MIN_VALUE;  
                i++;  
            } else {  
                limit = -Integer.MAX_VALUE;  
            }  
            multmin = limit / radix;  
            if (i < max) {  
                digit = Character.digit(s.charAt(i++),radix);  
                if (digit < 0) {  
                    throw NumberFormatException.forInputString(s);  
                } else {  
                    result = -digit;  
                }  
            }  
            while (i < max) {  
            // 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);  
        }  
        if (negative) {  
            if (i > 1) {  
                return result;  
            } else {    /* Only got "-" */  
                throw NumberFormatException.forInputString(s);  
            }  
        } else {  
            return -result;  
        }  
    }  

      

  • 相关阅读:
    cisco 4500X 交换机限速
    HPE 交换机基础配置
    MySQL数据库之主从复制
    MySQL复制线程状态转变
    MySQL数据库备份之mysqldump
    MySQL数据库之慢查询日志
    MySQL数据库之多线程备份工具mydumper
    MySQL数据库之索引
    MySQL之二进制日志
    MySQL数据库之sql_mode解释
  • 原文地址:https://www.cnblogs.com/juihai/p/8948508.html
Copyright © 2011-2022 走看看