80,362
社区成员
发帖
与我相关
我的任务
分享
import java.lang.reflect.Field;
public class Main {
public static void method(int a, int b) throws Exception
{
Class cache = Integer.class.getDeclaredClasses()[0];
Field c = cache.getDeclaredField("cache");
c.setAccessible(true);
Integer[] array = (Integer[]) c.get(cache);
array[138] = 100;
array[148] = 200;
}
public static void main(String[] args) throws Exception {
int a = 10; int b = 20;
method(a, b);
System.out.printf("a=%d b=%d",a, b);
}
}
Integer在内部维持了一个IntegerCache, 用于缓存 -128 到127的数。
http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/java/lang/Integer.java#Integer.IntegerCache
private static class IntegerCache {
private IntegerCache(){}
static final Integer cache[] = new Integer[-(-128) + 127 + 1];
static {
for(int i = 0; i < cache.length; i++)
cache[i] = new Integer(i - 128);
}
}
上面的代码变是把这个cache数组给改了!当然,实际代码中这么写,那是肯定不行的!!!
///