Java-API简析_java.lang.Integer类(基于 Latest JDK)(浅析源码)

【版权声明】未经博主同意,谢绝转载!(请尊重原创,博主保留追究权)
https://blog.csdn.net/m0_69908381/article/details/130730986
出自【进步*于辰的博客

注:字符类:Character

文章目录

1、概述

继承关系:

  • java.lang.Object
    • java.lang.Number
      • java.lang.Integer

所有已实现的接口:
Serializable, Comparable<Integer>


public final class Integer extends Number implements Comparable<Integer>

Integer 类在对象中包装了一个基本类型 int 的值。Integer 类型的对象包含一个 int 类型的字段。

此外,该类提供了多个方法,能在 int 类型和 String 类型之间互相转换,还提供了处理 int 类型时非常有用的其他一些常量和方法。

实现注意事项:“bit twiddling()”(如 highestOneBit()numberOfTrailingZeros())的实现基于 Henry S. Warren, Jr.撰写的 《Hacker’s Delight》(Addison Wesley, 2002)中的一些有关材料。

从以下版本开始:
JDK1.0
另请参见:
序列化表格

2、字段摘要

2.1 static int MAX_VALUE

保持 int 类型的最大值的常量可取的值为 231-1。

public static final int   MAX_VALUE = 0x7fffffff;

推荐一篇博文《二进制相关概念、运算与应用》,从中可知八位有符号二进制的表示范围是-128 ~ 127

127 = 27 - 1。整型占4个字节,共32位,故最大值为 231-1。

为何 \color{grey}{为何} 为何 231-1 的十六进制是 \color{grey}{的十六进制是} 的十六进制是 0x7fffffff?同样以127为例。127 = 7 * 161 + 15,故127的十六进制为0x7f

2.2 static int MIN_VALUE

保持 int 类型的最小值的常量可取的值为 -231

public static final int   MIN_VALUE = 0x80000000;

从上1项可知,8位有符号二进制的最小值是-128,而 -128 = -27,其十六进制为0x80,其原码是1000 0000

2.3 static int SIZE

以二进制补码形式表示 int 值的位数。

public static final int SIZE = 32;// 占4个字节

2.4 static Class<Integer> TYPE

表示基本类型 int 的 Class 实例。

public static final Class<Integer>  TYPE = (Class<Integer>) Class.getPrimitiveClass("int");

关于getPrimitiveClass(),见Class<T>的第3.1项,即返回int.class

3、构造方法摘要

属性说明:

private final int value;

3.1 int value

构造一个新分配的 Integer 对象,它表示指定的 int 值。

3.2 String s

构造一个新分配的 Integer 对象,它表示 String 参数所指示的 int 值。

public Integer(String s) throws NumberFormatException {
	// 将字符串s转为数字,故字符串中的所有字符必须全为数字,否则异常
    this.value = parseInt(s, 10);// 调用第4.17项
}

几乎所有数值型包装类都有这个构造方法,且底层都如此。

4、方法摘要

4.1 static int bitCount(int i)

返回指定 int 值的二进制补码表示形式的 1 位的数量。

public static int bitCount(int i) {
    // HD, Figure 5-2
    i = i - ((i >>> 1) & 0x55555555);
    i = (i & 0x33333333) + ((i >>> 2) & 0x33333333);
    i = (i + (i >>> 4)) & 0x0f0f0f0f;
    i = i + (i >>> 8);
    i = i + (i >>> 16);
    return i & 0x3f;
}

后续解析。

示例:

Integer.bitCount(1);// 1
Integer.bitCount(Integer.MAX_VALUE);// 31

Integer.bitCount(-1);// 32
Integer.bitCount(Integer.MIN_VALUE);// 1

4.2 byte byteValue()

以 byte 类型返回该 Integer 的值。

4.3 int compareTo(Integer anotherInteger)

在数字上比较两个 Integer 对象。

public int compareTo(Integer anotherInteger) {
    return compare(this.value, anotherInteger.value);// 调用第20项
}

4.4 static Integer decode(String nm)

将 String 解码为 Integer。

public staticInteger decode(String nm) throws NumberFormatException {
    int radix = 10;
    int index = 0;
    boolean negative = false;// 是否为负
    Integer result;

    if (nm.isEmpty())// 判断是否是空字符串
        throw new NumberFormatException("Zero length string");
    
    // 判断首字符是否是 +/- (符号)
	char firstChar = nm.charAt(0);
    // Handle sign, if present
    if (firstChar == '-') {
        negative = true;
        index++;
    } else if (firstChar == '+')
        index++;

    // Handle radix specifier, if present
    if (nm.startsWith("0x", index) || nm.startsWith("0X", index)) {// 判断是否是16进制
        index += 2;
        radix = 16;
    }
    else if (nm.startsWith("#", index)) {// 定义以 # 开头是16进制
        index ++;
        radix = 16;
    }
    else if (nm.startsWith("0", index) && nm.length() > 1 + index) {// 判断是否是8进制
        index ++;
        radix = 8;
    }

	// 符号不在首位或有多个符号,定义为异常
    if (nm.startsWith("-", index) || nm.startsWith("+", index))
        throw new NumberFormatException("Sign character in wrong position");

    try {
        result = Integer.valueOf(nm.substring(index), radix);// valueOf()见第35项
        result = negative ? Integer.valueOf(-result.intValue()) : result;// valueOf()见第33项
    } catch (NumberFormatException e) {
        // 当数为 Integer.MIN_VALUE 时,转换异常
        String constant = negative ? ("-" + nm.substring(index))
                                   : nm.substring(index);
        result = Integer.valueOf(constant, radix);
    }
    return result;
}

示例:

Integer.decode("-10");// -10
Integer.decode("" + Integer.MIN_VALUE);// -2147483648
Integer.decode("+010");// 8
Integer.decode("+0x10");// 16

可认为此方法是基于parseInt()(第19项)的封装。

4.5 double doubleValue()

以 double 类型返回该 Integer 的值。

4.6 boolean equals(Object obj)

比较此对象与指定对象。

public boolean equals(Object obj) {
    if (obj instanceof Integer) {
        return value == ((Integer)obj).intValue();
    }
    return false;
}

4.7 float floatValue()

以 float 类型返回该 Integer 的值。

4.8 static Integer getInteger(String nm)

确定具有指定名称的系统属性的整数值。

public static IntegergetInteger(String nm) {
    return getInteger(nm, null);// 见第10项
}

4.9 static Integer getInteger(String nm, int val)

确定具有指定名称的系统属性的整数值。

public static Integer getInteger(String nm, int val) {
    Integer result = getInteger(nm, null);// 见下1项
    return (result == null) ? Integer.valueOf(val) : result;// 见第33项
}

4.10 static Integer getInteger(String nm, Integer val)

确定具有指定名称的系统属性的整数值。

public static Integer getInteger(String nm, Integer val) {
    String v = null;
    try {
        v = System.getProperty(nm);// 获取系统属性
    } catch (IllegalArgumentException | NullPointerException e) {
    }
    if (v != null) {
        try {
            return Integer.decode(v);// 解码,String → int,见第4项
        } catch (NumberFormatException e) {
        }
    }
    return val;
}

getProperty()System类的第2.8项、

示例:

// 系统配置:
sun.arch.data.model	64
user.language	zh

System.out.println(Integer.getInteger("sun.arch.data.model"));// 64
System.out.println(Integer.getInteger("user.language", 400));// 400

4.11 int hashCode()

返回此 Integer 的哈希码。

public int hashCode() {
    return Integer.hashCode(value);
}

public static int hashCode(int value) {
    return value;
}

4.12 static int highestOneBit(int i)

返回具有至多单个 1 位的 int 值,在指定的 int 值中最高位(最左边)的 1 位的位置。

public static int highestOneBit(int i) {
    // HD, Figure 3-1
    i |= (i >>  1);
    i |= (i >>  2);
    i |= (i >>  4);
    i |= (i >>  8);
    i |= (i >> 16);
    return i - (i >>> 1);
}

原理: \color{red}{原理:} 原理:(以23为例)

23的补码:0001 0111
i = 23 | (23 >> 1) = 0001 0111 | 0000 1011 = 0001 1111 = 31
i = 31 | (31 >> 2) = 0001 1111 | 0000 0111 = 0001 1111 = 31
i = 31 | (31 >> 4) = 0001 1111 | 0000 0001 = 0001 1111 = 31
i = 31 | (31 >> 8) = 0001 1111 | 0000 0000 = 0001 1111 = 31
i = 31 | (31 >> 16) = 0001 1111 | 0000 0000 = 0001 1111 = 31
i - (i >>> 1) = 0001 1111 - 0000 1111 = 0001 0000 = 16
因此,最高位是第5位。

位运算|可保留最高位,在多次>>|=后,最高位后的每一位都会变为1。因此,最后相减的结果,只剩下最高位的1

为什么 为什么 为什么>> 的位数都是 2 的指数,且是五次? \color{grey}{的位数都是2的指数,且是五次?} 的位数都是2的指数,且是五次?(以 230 为例)

01000000 00000000 00000000 00000000
01100000 00000000 00000000 00000000
01111000 00000000 00000000 00000000
01111111 10000000 00000000 00000000
01111111 11111111 10000000 00000000
01111111 11111111 11111111 11111111
01000000 00000000 00000000 00000000

“2的指数”是为了保证最高位后的每一位都为1,“五次”是因为最高位后最多有30位。

若·i·是负数,为什么结果都相同?因为>>

4.13 int intValue()

以 int 类型返回该 Integer 的值。

4.14 static int lowestOneBit(int i)

返回具有至多单个 1 位的 int 值,在指定的 int 值中最低位(最右边)的 1 位的位置。

public static int lowestOneBit(int i) {
    return i & -i;
}

测试:

1 & -1 = 0000 0001 & 1111 1111 = 0000 0001 = 1
2 & -2 = 0000 0010 & 1111 1110 = 0000 0010 = 2
4 & -4 = 0000 0100 & 1111 1100 = 0000 0100 = 4
8 & -8 = 0000 1000 & 1111 1000 = 0000 1000 = 8
16 & -16 = 0001 0000 & 1111 0000 = 0001 0000 = 16

127 & -127 = 0111 1111 & 1000 0001 = 0000 0001 = 1
126 & -126 = 0111 1110 & 1000 0010 = 0000 0010 = 2
125 & -125 = 0111 1101 & 1000 0011 = 0000 0001 = 1
124 & -124 = 0111 1100 & 1000 0100 = 0000 0100 = 4
123 & -123 = 0111 1011 & 1000 0101 = 0000 0001 = 1

其中原理暂不清楚,需要大家自行推敲。

4.15 static int numberOfLeadingZeros(int i)

在指定 int 值的二进制补码表示形式中最高位(最左边)的 1 位之前,返回零位的数量。

public static int numberOfLeadingZeros(int i) {
    // HD, Figure 5-6
    if (i == 0)
        return 32;
    int n = 1;
    if (i >>> 16 == 0) { n += 16; i <<= 16; }
    if (i >>> 24 == 0) { n +=  8; i <<=  8; }
    if (i >>> 28 == 0) { n +=  4; i <<=  4; }
    if (i >>> 30 == 0) { n +=  2; i <<=  2; }
    n -= i >>> 31;
    return n;
}

原理: \color{red}{原理:} 原理:(为了保证第一个iftrue,以一个极端的数字 215 为例)

i的补码:00000000 00000000 10000000 00000000
只有第一个if生效,
n为17,i为:10000000 00000000 00000000 00000000
n = n - (i >>> 31) = 17 - (00000000 00000000 00000000 00000001) = 16

为何如此能实现?又为何分别是16、24、28、30?后续说明。

负数的结果都为 \color{grey}{负数的结果都为} 负数的结果都为0 ,是如何运算的? \color{grey}{,是如何运算的?} ,是如何运算的?后续说明。

4.16 static int numberOfTrailingZeros(int i)

返回指定的 int 值的二进制补码表示形式中最低(“最右”)的为 1 的位后面的零位个数。

public static int numberOfTrailingZeros(int i) {
    // HD, Figure 5-14
    int y;
    if (i == 0) return 32;
    int n = 31;
    y = i <<16; if (y != 0) { n = n -16; i = y; }
    y = i << 8; if (y != 0) { n = n - 8; i = y; }
    y = i << 4; if (y != 0) { n = n - 4; i = y; }
    y = i << 2; if (y != 0) { n = n - 2; i = y; }
    return n - ((i << 1) >>> 31);
}

原理: \color{red}{原理:} 原理:(为了保证所有if都为true,以一个极端的数字 1 为例)

1的补码:00000000 00000000 00000000 00000001,n = 31
第一个if:n = 15, i = 00000000 00000001 00000000 00000000
第二个if:n = 7, i = 00000001 00000000 00000000 00000000
第三个if:n = 3, i = 00010000 00000000 00000000 00000000
第四个if:n = 1, i = 01000000 00000000 00000000 00000000
n - ((i << 1) >>> 31) = n - 00000000 00000000 00000000 00000001 = 0

为何如此能实现?又为何分别是16、8、4、2?后续说明。

4.17 static int parseInt(String s)

将字符串参数作为有符号的十进制整数进行分析。

public staticint parseInt(String s) throws NumberFormatException {
    return parseInt(s,10);// 10 表示十进制,即 String → int,调用第19项
}

4.18 long longValue()

以 long 类型返回该 Integer 的值。

4.19 static int parseInt(String s, int radix)

将字符串参数按照指定进制进行分析,返回十进制结果。

public static int parseInt(String s, int radix)
            throws NumberFormatException
    if (s == null) {
        throw new NumberFormatException("null");
    }

	// 进制 radix 的范围是 2 ~ 36
    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();
    // 最终返回十进制整数,故 s 对应的十进制必须在整数范围内 
    int limit = -Integer.MAX_VALUE;
    int multmin;
    int digit;

    if (len > 0) {
        char firstChar = s.charAt(0);
        // s 的第一个字符可以是符号,但只能是 + / -
        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++;
        }
        
        // 大概用于限制 radix 进制下 s 的范围
        multmin = limit / radix;
        while (i < len) {
            // Accumulating negatively avoids surprises near MAX_VALUE
            digit = Character.digit(s.charAt(i++),radix);// digit()见第5.13项
            if (digit < 0) {
                throw NumberFormatException.forInputString(s);
            }
            if (result < multmin) {// ------A
                throw NumberFormatException.forInputString(s);
            }
            result *= radix;// ------B
            if (result < limit + digit) {// ------C
                throw NumberFormatException.forInputString(s);
            }
            result -= digit;// ------D
        }
    } else {
        throw NumberFormatException.forInputString(s);
    }
    return negative ? result : -result;
}

此方法将“数字型”字符串转换成十进制整数的方法就是 B 和 D,举个例:

s 是 “a1”,radix 是 16,即将"a1"视为十六进制,转换成十进制整数
换算:“a1” → 10 * 161 + 1 * 160 = 161

B 和 D 的逻辑就是如此,只是采用了公倍数的小技巧,大家自行推演一遍就明白了。

A 和 C 的作用大概是限制范围,暂不理解,后续补充解析。

示例。

Integer.parseInt("11111111", 2);// 255
Integer.parseInt("-377", 8);// -255
Integer.parseInt("+ff", 16);// 255

4.20 static int compare(int x, int y)

比较两个 int 类型值的大小关系。

public static intcompare(int x, int y) {
    return (x < y) ? -1 : ((x == y) ? 0 : 1);
}

4.21 static int reverse(int i)

返回通过反转指定 int 值的二进制补码表示形式中位的顺序而获得的值。

4.22 static int reverseBytes(int i)

返回通过反转指定 int 值的二进制补码表示形式中字节的顺序而获得的值。

4.23 static int rotateLeft(int i, int distance)

返回根据指定的位数循环左移指定的 int 值的二进制补码表示形式而得到的值。

4.24 static int rotateRight(int i, int distance)

返回根据指定的位数循环右移指定的 int 值的二进制补码表示形式而得到的值。

4.25 short shortValue()

以 short 类型返回该 Integer 的值。

4.26 static int signum(int i)

返回指定 int 值的符号函数。

public static int signum(int i) {
    return (i >> 31) | (-i >>> 31);
}

i是负数 / 零 / 正数,返回-1/0/1,说明:(i 为1

1 的补码:0000...0001
-1 的补码:1111...1111
1 >> 31 是 0000...0000
-1 >>> 31 是 0000...0001
结果:0000...0000 | 0000...0001 = 0000...0001 = 1

暂不知其中二进制运算规律。

4.27 static String toBinaryString(int i)

以二进制无符号整数形式(补码)返回一个整数参数的字符串表示形式。

public static String toBinaryString(int i) {
    return toUnsignedString0(i, 1);// 调用第 5.1项
}

示例:

Integer.toBinaryString(1);// 1
Integer.toBinaryString(Integer.MAX_VALUE);// 1111111 11111111 11111111 11111111
        
Integer.toBinaryString(-1);// 11111111 11111111 11111111 11111111
Integer.toBinaryString(Integer.MIN_VALUE);// 10000000 00000000 00000000 00000000

4.28 static String toHexString(int i)

以十六进制的无符号整数形式返回一个整数参数的字符串表示形式。

public static String toHexString(int i) {
    return toUnsignedString0(i, 4);// 调用第 5.1项
}

示例:

Integer.toHexString(1);// 1,无前缀 '0x'
Integer.toHexString(Integer.MAX_VALUE);// 7f ff ff ff
        
Integer.toHexString(-1);// ffffffff
Integer.toHexString(Integer.MIN_VALUE);// 80 00 00 00

4.29 static String toOctalString(int i)

以八进制(基数 8)无符号整数形式返回一个整数参数的字符串表示形式。

public static String toOctalString(int i) {
    return toUnsignedString0(i, 3);// 调用第 5.1项
}

示例:

Integer.toOctalString(1);// 1,无前缀 '0'
Integer.toOctalString(Integer.MAX_VALUE);// 17 777 777 777

Integer.toOctalString(-1);// 37 777 777 777
Integer.toOctalString(Integer.MIN_VALUE);// 20 000 000 000

4.30 String toString()

返回一个表示该 Integer 值的 String 对象。

public String toString() {
    return toString(value);// 调用下1项
}

4.31 static String toString(int i)

返回一个表示指定整数的 String 对象。

public static String toString(int i) {
    if (i == Integer.MIN_VALUE)
        return "-2147483648";
    int size = (i < 0) ? stringSize(-i) + 1 : stringSize(i);
    char[] buf = new char[size];
    getChars(i, size, buf);
    return new String(buf, true);
}

后续解析。

4.32 static String toString(int i, int radix)

用第二个参数指定的基数返回第一个参数的字符串表示形式。

4.33 static Integer valueOf(int i)

返回一个表示指定的 int 值的 Integer 实例。

public static Integer valueOf(int i) {
    if (i >= IntegerCache.low && i <= IntegerCache.high)
        return IntegerCache.cache[i + (-IntegerCache.low)];
    return new Integer(i);
}

Integer 常量存储于方法区的整型常量池,对应源码中的cache[]
IntegerCache是 Integer 类的私有静态内部类,可猜测 Integer 类就是通过此类与整型常量池对接。经过测试,发现 IntegerCache 类无法进行debug,故暂且不知其底层实现,后续补充。

扩展说明 \color{red}{扩展说明} 扩展说明low的值为-128high127。若 i 的值在此范围内,返回整型常量池的元素,否则返回 Integer 实例。这就是 Integer 类的“自动装箱”和“自动拆箱”的底层逻辑。

4.34 static Integer valueOf(String s)

返回保持指定的 String 的值的 Integer 对象。

public static Integer valueOf(String s) throws NumberFormatException {
    return Integer.valueOf(parseInt(s, 10));// 分别见上1项、第17项
}

4.35 static Integer valueOf(String s, int radix)

返回一个 Integer 对象,该对象中保持了用第二个参数提供的基数进行分析时从指定的 String 中提取的值。

public static Integer valueOf(String s, int radix) throws NumberFormatException {
    return Integer.valueOf(parseInt(s,radix));// 分别见第33项、第17项
}

4.36 static int parseUnsignedInt(String s)

返回指定字符串对应的十进制数。

public static int parseUnsignedInt(String s) throws NumberFormatException {
    return parseUnsignedInt(s, 10);// 调用下1项
}

4.37 static int parseUnsignedInt(String s, int radix)

返回指定字符串中指定进制数转换的十进制数。

public static int parseUnsignedInt(String s, int radix)
            throws NumberFormatException {
    if (s == null)  {
        throw new NumberFormatException("null");
    }

    int len = s.length();
    if (len > 0) {
        char firstChar = s.charAt(0);
        if (firstChar == '-') {
            throw new
                NumberFormatException(String.format("Illegal leading minus sign " +
                                                   "on unsigned string %s.", s));
        } else {
            if (len <= 5 || // Integer.MAX_VALUE in Character.MAX_RADIX is 6 digits
                (radix == 10 && len <= 9) ) { // Integer.MAX_VALUE in base 10 is 10 digits
                return parseInt(s, radix);
            } else {
                long ell = Long.parseLong(s, radix);
                if ((ell & 0xffff_ffff_0000_0000L) == 0) {
                    return (int) ell;
                } else {
                    throw new
                        NumberFormatException(String.format("String value %s exceeds " +
                                                            "range of unsigned int.", s));
                }
            }
        }
    } else {
        throw NumberFormatException.forInputString(s);
    }
}

后续解析。

示例:

Integer.parseUnsignedInt("1010", 2);// 10
Integer.parseUnsignedInt("12", 8);// 10
Integer.parseUnsignedInt("a", 16);// 10

不能指定前缀,如十六进制的前缀0x

5、方法摘要(不开放)

5.1 private static String toUnsignedString0(int val, int shift)

以二进制(基数 2)无符号整数形式返回一个整数参数的字符串表示形式。

private static String toUnsignedString0(int val, int shift) {
    // assert shift > 0 && shift <=5 : "Illegal shift value";
    int mag = Integer.SIZE - Integer.numberOfLeadingZeros(val);// 返回最高位的 1 位之前 0 位的个数,见第4.15项

	// shift 经过运算,可得到最终返回的进制数的长度和 radix
    int chars = Math.max(((mag + (shift - 1)) / shift), 1);
    char[] buf = new char[chars];

    formatUnsignedInt(val, shift, buf, 0, chars);// 将val转为相应进制数逐一赋值于buf

    return new String(buf, true);
}

后续补充解析。

最后

如果大家需要Java-API文档,我上传了《Java-API文档-包含5/8/11三个版本》。


本文暂缓更新。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

进步·于辰

谢谢打赏!!很高兴可以帮到你!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值