import java.io.*;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
class test
{html
public static void main(String []args) {
Integer a = 1000;java
Integer b = 2000;缓存
swap(a, b); //实现该函数,使得a,b的值交换函数
Integer c=1000;
.net
System.out.println(a + "-" + b + "-" + a.getClass()+" c:"+c); //2000-1000-class java.lang.Integer c:1000htm
/*若是a=1,b=2,c=1则输出结果中的c会变成2,缘由参考Interger缓存(-128到127),修改了缓存里的值致使1变成2了*/blog
}get
static void swap(Integer a, Integer b) {
int temp = a;io
try {
Class clazz = a.getClass();
Field value = clazz.getDeclaredField("value");
value.setAccessible(true);
value.setInt(a, b);
value.setInt(b, temp);
} catch (Exception ex) {
ex.printStackTrace();
}class
}
http://www.blogjava.net/sundc/archive/2013/06/08/400382.html
From:https://www.cnblogs.com/xuejianxiyang/p/9155397.html