代碼如下:package basic;import java.lang.reflect.Field;public class TestField { public static void main(String[] args) throws NoSuchFieldException, IllegalAccessException {
@SuppressWarnings("rawtypes")
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;
System.out.printf("%d + %d = %d", a, a, b);
}
}運行結(jié)果如下:2 + 2 = 5為什么會有這樣的輸出結(jié)果呢?另外附上內(nèi)部類IntgerCache的源碼如下:
Java中反射計算2 + 2 = 5
30秒到達戰(zhàn)場
2018-08-08 14:14:20
