源码解析 - JDK - parseInt(String s, int radix)

/**
     * Parses the string argument as a signed integer in the radix
     * specified by the second argument. The characters in the string
     * must all be digits of the specified radix (as determined by
     * whether {@link java.lang.Character#digit(char, int)} returns a
     * nonnegative value), except that the first character may be an
     * ASCII minus sign {@code '-'} ({@code '\u005Cu002D'}) to
     * indicate a negative value or an ASCII plus sign {@code '+'}
     * ({@code '\u005Cu002B'}) to indicate a positive value. The
     * resulting integer value is returned.
     * 根据第二个参数radix传入的进制数将该字符串转化为一个整数类型。字符串中的字符必须全部为
     * 特定进制的数字(全数字表示返回一个非负数),除非第一个字符可能是ASCII的负号{@code '-'}                    
     * ({@code '\u005Cu002D'})表示一个负数或者一个ASCII的正号{@code '+'}({@code 
     * '\u005Cu002B'})表示一个正数。整数结果以返回值的形式返回。
     *
     * 

An exception of type {@code NumberFormatException} is * thrown if any of the following situations occurs: * 如果一下任何一个情况发生会抛出一个类型为{@code NumberFormatException}(数字格式) * 的异常。 * *

    *
  • The first argument is {@code null} or is a string of * length zero. * 情况一:第一个参数为空或者是一个长度为0的字符串。 * *
  • The radix is either smaller than * {@link java.lang.Character#MIN_RADIX} or * larger than {@link java.lang.Character#MAX_RADIX}. * 情况二:参数radix小于{@link java.lang.Character#MIN_RADIX}或者 * 大于{@link java.lang.Character#MAX_RADIX} * *
  • Any character of the string is not a digit of the specified * radix, except that the first character may be a minus sign * {@code '-'} ({@code '\u005Cu002D'}) or plus sign * {@code '+'} ({@code '\u005Cu002B'}) provided that the * string is longer than length 1. * 情况三:任意一个字符不是特定进制的数字,除了长度大于1的字符串的第一个字符 * 可能为负号或者正号。 * *
  • The value represented by the string is not a value of type * {@code int}. *
* 该字符串代表的值不是一个{@code int}(整数)类型的值。 * *

Examples: *

     * parseInt("0", 10) returns 0
     * parseInt("473", 10) returns 473
     * parseInt("+42", 10) returns 42
     * parseInt("-0", 10) returns 0
     * parseInt("-FF", 16) returns -255
     * parseInt("1100110", 2) returns 102
     * parseInt("2147483647", 10) returns 2147483647
     * parseInt("-2147483648", 10) returns -2147483648
     * parseInt("2147483648", 10) throws a NumberFormatException
     * parseInt("99", 8) throws a NumberFormatException
     * parseInt("Kona", 10) throws a NumberFormatException
     * parseInt("Kona", 27) returns 411787
     * 
* * @param s the {@code String} containing the integer * representation to be parsed * @param radix the radix to be used while parsing {@code s}. * @return the integer represented by the string argument in the * specified radix. * @exception NumberFormatException if the {@code String} * does not contain a parsable {@code int}. */ 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;//定义是否为负数的flag,true表示为负数 int i = 0, len = s.length();//定义计数器以及计数器的上限 int limit = -Integer.MAX_VALUE;//获取Integer类型的最大值,将其反转为负数 int multmin;//设定转换后的整数的最大值,因为以负数计算的所以是min int digit;//定义为字符串中每一位字符的存储变量 if (len > 0) {//判断字符串长度是否大于0 char firstChar = s.charAt(0);//获取字符串的第一个字符 if (firstChar < '0') { //判断第一个字符是否是正负号(通过Unicode码) if (firstChar == '-') {//第一个字符是负号的情况 negative = true;//设置为负数 limit = Integer.MIN_VALUE; } else if (firstChar != '+') //如果第一个字符既不是数字也不是正负号则抛异常 throw NumberFormatException.forInputString(s); if (len == 1) //如果长度为1且第一个字符为正负号则抛异常 throw NumberFormatException.forInputString(s); i++;//第一个字符为正负号,所以计数器从第二个字符开始算数字 } //计算特定进制的数字的最大值 multmin = limit / radix; while (i < len) {//计数器不到上限持续循环 //每个i位置的字符都转换为对应radix的进制的数字 digit = Character.digit(s.charAt(i++),radix); if (digit < 0) { //如果转换过来的数字小于0则抛出异常 throw NumberFormatException.forInputString(s); } if (result < multmin) { //判断结果是否超过了最大范围,因为是以负数去计算的,所以反转过来判断小于最小值 throw NumberFormatException.forInputString(s); } //每循环一次就乘以一次进制数,表示位数高一位,例如十位变成百位 result *= radix; if (result < limit + digit) { //判断结果是否超过了最大范围,因为是以负数去计算的,所以反转过来判断小于最小值 throw NumberFormatException.forInputString(s); } //之前已经乘以了进制数,所以每一次循环到这个位都是0,加上当前的那一位即可,因为是以负数计算的,所以是减digit result -= digit; } } else { throw NumberFormatException.forInputString(s); } //通过正负数的flag来判断是否正负反转结果整数 return negative ? result : -result; }

 

你可能感兴趣的:(源码解析)