一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

10 Integer:深入剖析最常用的整数包装类

时间:2026-06-01 15:40:01 编辑:袖梨 来源:一聚教程网

Integer作为Java最基础的包装类,其设计精妙与性能优化值得开发者深入探究。本文将全面剖析其核心机制与实战应用场景。

Integer —— 最常用的整数包装类深度解析


一、Integer 的类结构

public final class Integer extends Number implements Comparable {    @Native public static final int MIN_VALUE = 0x80000000;   // -2^31
    @Native public static final int MAX_VALUE = 0x7fffffff;   // 2^31 - 1    public static final Class TYPE
        = (Class) Class.getPrimitiveClass("int");    public static final int SIZE = 32;
    public static final int BYTES = SIZE / Byte.SIZE;    private final int value;    private static final long serialVersionUID = 1360826667806852920L;
}

二、IntegerCache —— 面试必问的缓存机制

private static class IntegerCache {
    static final int low = -128;
    static final int high;
    static final Integer cache[];    static {
        int h = 127;
        String integerCacheHighPropValue =
            sun.misc.VM.getSavedProperty("java.lang.Integer.IntegerCache.high");
        if (integerCacheHighPropValue != null) {
            try {
                int i = parseInt(integerCacheHighPropValue);
                i = Math.max(i, 127);
                // 防止缓存数组过大
                h = Math.min(i, Integer.MAX_VALUE - (-low) - 1);
            } catch (NumberFormatException nfe) {
                // 配置错误则忽略
            }
        }
        high = h;
        cache = new Integer[(high - low) + 1];
        int j = low;
        for (int k = 0; k < cache.length; k++) {
            cache[k] = new Integer(j++);
        }
    }    private IntegerCache() {}
}

缓存生效场景

public class IntegerCacheDemo {
    public static void main(String[] args) {
        Integer a = 127;
        Integer b = 127;
        System.out.println(a == b);  // true——都是缓存中同一对象        Integer c = 128;
        Integer d = 128;
        System.out.println(c == d);  // false——超出缓存范围,各自 new        // 显式调用 valueOf 同样走缓存
        System.out.println(Integer.valueOf(100) == Integer.valueOf(100)); // true        // new Integer 始终不走缓存
        System.out.println(new Integer(100) == new Integer(100)); // false
    }
}

调整缓存上限

# 设置缓存上限为 1000
java -Djava.lang.Integer.IntegerCache.high=1000 MyApp

三、自动装箱与拆箱

public class BoxingDemo {
    public static void main(String[] args) {
        Integer boxed = 42;   // 等价于 Integer.valueOf(42)        int unboxed = boxed;  // 等价于 boxed.intValue()        Integer x = 100, y = 200;
        Integer z = x + y;    // 拆箱 → 计算 → 装箱        Integer n1 = null;
        // 下面这行会抛出 NullPointerException(n1 拆箱为 int 时)
        // int result = true ? n1 : 0;
    }    // 实际编译后的字节码等价于:
    // Integer boxed = Integer.valueOf(42);
    // int unboxed = boxed.intValue();
}

四、进制转换方法

4.1 toString(int i, int radix)

public static String toString(int i, int radix) {
    if (radix < Character.MIN_RADIX || radix > Character.MAX_RADIX)
        radix = 10;
    if (radix == 10) return toString(i);
    char buf[] = new char[33];
    boolean negative = (i < 0);
    int charPos = 32;
    if (!negative) i = -i;
    while (i <= -radix) {
        buf[charPos--] = digits[-(i % radix)];
        i = i / radix;
    }
    buf[charPos] = digits[-i];
    if (negative) buf[--charPos] = '-';
    return new String(buf, charPos, (33 - charPos));
}
public class RadixDemo {
    public static void main(String[] args) {
        int val = 255;
        System.out.println("十进制: " + Integer.toString(val, 10));  // 255
        System.out.println("二进制: " + Integer.toString(val, 2));   // 11111111
        System.out.println("八进制: " + Integer.toString(val, 8));   // 377
        System.out.println("十六进制: " + Integer.toString(val, 16)); // ff
        System.out.println("三十二进制: " + Integer.toString(val, 32)); // 7v
    }
}

4.2 toHexString / toOctalString / toBinaryString

public static String toHexString(int i) {
    return toUnsignedString0(i, 4);  // 4位一组 → 十六进制
}public static String toOctalString(int i) {
    return toUnsignedString0(i, 3);  // 3位一组 → 八进制
}public static String toBinaryString(int i) {
    return toUnsignedString0(i, 1);  // 1位一组 → 二进制
}

五、parseInt —— 字符串解析精要

public static int parseInt(String s, int radix) throws NumberFormatException {
    if (s == null) throw new NumberFormatException("null");
    if (radix < Character.MIN_RADIX)
        throw new NumberFormatException("radix " + radix + " less...");
    if (radix > Character.MAX_RADIX)
        throw new NumberFormatException("radix " + radix + " greater...");    int result = 0;
    boolean negative = false;
    int i = 0, len = s.length();
    int limit = -Integer.MAX_VALUE;    if (len > 0) {
        char firstChar = s.charAt(0);
        if (firstChar < '0') {
            if (firstChar == '-') { negative = true; limit = Integer.MIN_VALUE; }
            else if (firstChar != '+') throw NumberFormatException.forInputString(s);
            if (len == 1) throw NumberFormatException.forInputString(s);
            i++;
        }
        int multmin = limit / radix;
        while (i < len) {
            int 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;
}

设计亮点:使用负累加而非正累加,避免 Integer.MIN_VALUE 取负时的溢出问题。

10 Integer —— 最常用的整数包装类深度解析


六、位运算方法

6.1 highestOneBit / lowestOneBit

public static int highestOneBit(int i) {
    i |= (i >> 1);
    i |= (i >> 2);
    i |= (i >> 4);
    i |= (i >> 8);
    i |= (i >> 16);
    return i - (i >>> 1);
}public static int lowestOneBit(int i) {
    return i & -i;  // 利用补码特性
}
public class BitDemo {
    public static void main(String[] args) {
        int val = 18;  // 二进制: 00010010        System.out.println("highestOneBit(18): " + Integer.highestOneBit(val));  // 16
        System.out.println("lowestOneBit(18):  " + Integer.lowestOneBit(val));    // 2        // highestOneBit 的应用:快速找到 <= n 的最大2的幂
        System.out.println("2^{log2(100)}: " + Integer.highestOneBit(100)); // 64
    }
}

6.2 bitCount —— 统计 1 的个数

public static int bitCount(int i) {
    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;
}

6.3 numberOfLeadingZeros / numberOfTrailingZeros

public static int numberOfLeadingZeros(int i) {
    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;
}

七、decode —— 多进制解码器

public static Integer decode(String nm) throws NumberFormatException

解码规则:

前缀进制示例
0x / 0X十六进制0xFF → 255
#十六进制#FF → 255
0八进制0377 → 255
无前缀十进制255 → 255
public class DecodeDemo {
    public static void main(String[] args) {
        System.out.println(Integer.decode("255"));    // 255
        System.out.println(Integer.decode("0xFF"));   // 255
        System.out.println(Integer.decode("0XFF"));   // 255
        System.out.println(Integer.decode("#FF"));    // 255
        System.out.println(Integer.decode("-0xFF"));  // -255
        System.out.println(Integer.decode("0377"));