JDK学习篇

Posted 安小

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JDK学习篇相关的知识,希望对你有一定的参考价值。

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学习篇的主要内容,如果未能解决你的问题,请参考以下文章

JDK源码学习--String篇 关于String采用final修饰的思考

java学习随笔入门篇

配置 VScode 编辑器 (前端篇)

设计模式学习笔记之 开山篇

小白艰苦学习之路(记录学习历程——Java篇)Day2

小白艰苦学习之路(记录学习历程——Java篇)Day2