码迷,mamicode.com
首页 > 其他好文 > 详细

JDK学习篇

时间:2017-03-15 13:37:59      阅读:150      评论:0      收藏:0      [点我收藏+]

标签:integer   one   pre   microsoft   ros   ==   back   ffffff   else   

1. Integer.class

 

 parseInt

 

a. 充分考虑各种异常情况:字符串为空,带符号,进制出界,计算值出界

b. 计算时转换为负数进行处理

  Integer.MIN_VALUE直接变换符号会导致数值溢出

  Integer.MAX_VALUE = 0x7fffffff (+2147483647)
  Integer.MIN_VALUE = 0x80000000 (-2147483648, -10000000000000000000000000000000)
                                 = Integer.MAX_VALUE + 1 = Math.abs(Integer.MIN_VALUE) = -1 * Integer.    // 最大整数加1后等于最小整数
      public static int abs(int a) { return (a < 0) ? -a : a; }    // 注意如果参数等于Integer的最小值,将返回相同的值。

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

 

JDK学习篇

标签:integer   one   pre   microsoft   ros   ==   back   ffffff   else   

原文地址:http://www.cnblogs.com/anxiao/p/6553736.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!