IntegerCache

Integer Cache

  • IntegerCache源码
  • Integer 使用Cache源码
  • 瞎搞

IntegerCache源码


java.lang.Integer.IntegerCache {
     
        static final int low = -128;
        static final int high;
        static final Integer cache[];
        ...
}
  • Cache会缓存 [low, high-low] 范围内的整数
  • high默认127 范围 [127, Integer.MAX_VALUE - (-low) -1] , 可由 java.lang.Integer.IntegerCache.high 设置

Integer 使用Cache源码


public final class Integer extends Number implements Comparable<Integer> {
     
	...
	private final int value;
	...
	public static Integer valueOf(int i) {
     
		if (i >= IntegerCache.low && i <= IntegerCache.high)
			return IntegerCache.cache[i + (-IntegerCache.low)];
		return new Integer(i);
	}
	...
}
  • 可以看出非new的Integer如果值在[low, high]内, 则复用同一个对象
  • 注意java.lang.Integer#value为final

瞎搞


实现一段任何数字与Integer a = 2相乘为0的功能

/**
 * Integer常量池
 */
@Log4j2
public class ModifyInteger {
     

    static {
     
        try {
     
            modify();
        } catch (NoSuchFieldException | IllegalAccessException e) {
     
            e.printStackTrace();
        }
    }

    private static void modify() throws NoSuchFieldException, IllegalAccessException {
     
        // 1. 获取 IntegerCache 类
        Class<?> IntegerCache = Integer.class.getDeclaredClasses()[0];

        // 2. 获取 value 字段
        Field integerCacheValue = IntegerCache.getDeclaredField("cache");
        Field integerCacheLow = IntegerCache.getDeclaredField("low");

        // 3. 获取缓存数组
        integerCacheValue.setAccessible(true);
        Integer[] sourceCache = (Integer[]) integerCacheValue.get(Integer.class);

        // 4. 获取缓存最小值
        integerCacheLow.setAccessible(true);
        int low = integerCacheLow.getInt(Integer.class);

        // 5. 将缓存中的 2 修改为 0 (偷梁换柱)
        sourceCache[(low < 0 ? -low : low) + 2] = 0;
    }

    public static void check(Integer n) {
     
        Random random = new Random(System.currentTimeMillis());
        for (int idx = 0; idx < 10; idx++) {
     
        	// 14:48:53.775 [main] INFO org.example.codeTest.ModifyInteger - random * 2 = 0
            log.info("random * 2 = {}", random.nextInt(100) * n);
        }
    }
    
    public static void main(String[] args) {
     
        check(2);
    }
}

你可能感兴趣的:(java,java)