找回密码
 立即注册
首页 业界区 安全 JDK源码之Integer

JDK源码之Integer

王妍芳 2025-8-10 16:50:50
1、Integer类简介

  Integer是int的包装类,被final修饰,不能被其他类继承,继承Number类,Integer重写了相关方法,如longValue(),floatValue(),doubleValue()等等。Integer类实现了Compare接口。
2、源码

2.1、Integer类的属性
  1. /**
  2.      * int 类型能够表示的最小值 -2^31
  3.      * have, -2<sup>31</sup>.
  4.      */
  5.     @Native public static final int   MIN_VALUE = 0x80000000;
  6.     /**
  7.      * int 类型能够表示的最大值 2^31
  8.      * have, 2<sup>31</sup>-1.
  9.      */
  10.     @Native public static final int   MAX_VALUE = 0x7fffffff;
  11.     /**
  12.      * int 类型的 calss 实例
  13.      * {@code int}.
  14.      *
  15.      * @since   JDK1.1
  16.      */
  17.     @SuppressWarnings("unchecked")
  18.     public static final Class<Integer>  TYPE = (Class<Integer>) Class.getPrimitiveClass("int");/**
  19.      * 二进制补码形式下 int 的比特位数
  20.      * complement binary form.
  21.      *
  22.      * @since 1.5
  23.      */
  24.     @Native public static final int SIZE = 32;
  25.     /**
  26.      * 二进制补码下int的字节数/jdk1.8加入
  27.      * complement binary form.
  28.      *
  29.      * @since 1.8
  30.      */
  31.     public static final int BYTES = SIZE / Byte.SIZE;
复制代码
 

2.2、Integer类的构造方法
  1. public Integer(int value) {
  2.         this.value = value;
  3.     }
  4.     public Integer(String s) throws NumberFormatException {
  5.         this.value = parseInt(s, 10);
  6.     }
复制代码
共提供两个构造方法,一个参数为int,将int赋值给value。另外一个为String,通过parseInt方法将字符串转换为Integer。
2.3、parseInt()方法
  1. public static int parseInt(String s, int radix)
  2.                 throws NumberFormatException
  3.     {
  4.      //字符串为空 直接抛出异常
  5.         if (s == null) {
  6.             throw new NumberFormatException("null");
  7.         }
  8.       //小于最小数2 抛出异常 MAX_RADIX = 2  计算机的基础进制
  9.         if (radix < Character.MIN_RADIX) {
  10.             throw new NumberFormatException("radix " + radix +
  11.                                             " less than Character.MIN_RADIX");
  12.         }
  13.      //大于最大数36 抛出异常 MAX_RADIX = 36 表示0-9数字集a-z字母,共36字符
  14.         if (radix > Character.MAX_RADIX) {
  15.             throw new NumberFormatException("radix " + radix +
  16.                                             " greater than Character.MAX_RADIX");
  17.         }
  18.         int result = 0;<br>     //标志是否为负数 默认为false
  19.         boolean negative = false;<br>     //遍历初始位置i=0 , 字符串长度
  20.         int i = 0, len = s.length();
  21.         int limit = -Integer.MAX_VALUE;
  22.         int multmin;
  23.         int digit;
  24.      //字符串长度需要大于0 否者直接抛出异常
  25.         if (len > 0) {<br>       //字符串第一个字符
  26.             char firstChar = s.charAt(0);<br>       //第一个字符是否小于'0'的ASCII的值,说明不符合十进制的数字的合法开头。有两个例外 是'+'或'-',
  27.             if (firstChar < '0') { // Possible leading "+" or "-"
  28.                 if (firstChar == '-') { //如果是'-'
  29.                     negative = true; //负数标志变为true
  30.                     limit = Integer.MIN_VALUE;
  31.                 } else if (firstChar != '+')
  32.                     throw NumberFormatException.forInputString(s); //不是'+' 和 '-' 直接抛出异常
  33.                 if (len == 1) // Cannot have lone "+" or "-"   
  34.                     throw NumberFormatException.forInputString(s); //只有'+' 或 '-' 直接抛出异常
  35.                 i++;
  36.             }<br>       // 计算乘法前的最小值,用于后续溢出检查
  37.             multmin = limit / radix;
  38.             while (i < len) { //遍历字符串
  39.                 // Accumulating negatively avoids surprises near MAX_VALUE
  40.                 digit = Character.digit(s.charAt(i++),radix); //将字符转换为对应进制的数字
  41.                 if (digit < 0) { //非法数字 直接抛出异常
  42.                     throw NumberFormatException.forInputString(s);
  43.                 }
  44.                 if (result < multmin) { //乘法溢出检查
  45.                     throw NumberFormatException.forInputString(s);
  46.                 }
  47.                 result *= radix; //累积值乘以进制数
  48.                 if (result < limit + digit) { //检查加法的溢出
  49.                     throw NumberFormatException.forInputString(s);
  50.                 }
  51.                 result -= digit; //使用负数累积 防止MIN_VALUE 的绝对值比 MAX_VALUE大 1的情况  int的取值范围是-2147483648 至 2147483647
  52.             }
  53.         } else {
  54.             throw NumberFormatException.forInputString(s);
  55.         }
  56.         return negative ? result : -result; //转换回对应的整数结果
  57.     }
复制代码
2.4、valueOf()方法

[code]    public static Integer valueOf(String s, int radix) throws NumberFormatException {        return Integer.valueOf(parseInt(s,radix));    }    public static Integer valueOf(String s) throws NumberFormatException {        return Integer.valueOf(parseInt(s, 10));    }  // 根据传入的int 判断是否在缓存中,如果在,那么返回缓存中的数据,如果不在,则创建一个新的Integer对象返回    public static Integer valueOf(int i) {        if (i >= IntegerCache.low && i  Character.MAX_RADIX)            radix = 10;        /* Use the faster version */        if (radix == 10) {            return toString(i);        }        char buf[] = new char[33];        boolean negative = (i < 0);        int charPos = 32;        if (!negative) {            i = -i;        }        while (i = 2^16        while (i >= 65536) {            q = i / 100;        // 等价于 r = i - (q * 100);            r = i - ((q

相关推荐

您需要登录后才可以回帖 登录 | 立即注册