import java.lang.reflect.Field;
public class TestCache {
public static void main(String[] args)throws Exception {
Class[] cache1 = Integer.class.getDeclaredClasses();
Class cache = Integer.class.getDeclaredClasses()[0];
Field myCache = cache.getDeclaredField("cache");
myCache.setAccessible(true);
Integer[] newCache = (Integer[]) myCache.get(cache);
newCache[132] = newCache[133];
int a = 2;
int b = a + a;// b本来等于4 因为Integer 缓存里newCache[132]是等于4,被强制赋值成5,4的数组位置现在就是5。
System.out.printf("%d+ %d = %d ", a, a, b);
}
}
Integer中Cache的运用和原理
最新推荐文章于 2023-01-07 14:05:25 发布