The difference between Integer.valueof() and Integer.parseInt()

  When I was looking at the company code today, I saw that someone used the Integer.parseInt( String s ) method when converting String to  int . I have always used the Integer.valueOf( String s ) method. The parseInt() method was just I saw it in JavaScript, and I was a little interested, so I took a look at their source code.

  It is found that the bottom layer of Integer.valueof() and Integer.parseInt() is the Integer.parseInt(String s, int radix) method. This method is explained here.

  Integer.parseInt(String s, int radix), radix is ​​used to indicate what base the incoming value is, and returns the result of decimal  int  type . 

  For example, Integer.parseInt("A", 16), the output result is 10 in decimal, where 16 means "A" is a hexadecimal value.

  According to: Character.MIN_RADIX=2 and Character.MAX_RADIX=36 Then, the range of radix in the parseInt(String s, int radix) parameter is between 2 and 36, and an exception will be thrown if it exceeds the range. The length of s cannot exceed 7, otherwise an exception will be thrown. The limitation is within 36 bits because numbers and letters can just be represented to 36 bits, such as Integer.parseInt("Z", 36), the output result is 35.

  The following is the source code implementation of 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;  
    }  
}  

 

  

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324927969&siteId=291194637