import java.io.*;
import java.lang.reflect.Field;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
class test
{

public static void main(String []args) {
Integer a = 1000;

Integer b = 2000;

swap(a, b); //实现该函数,使得a,b的值交换

Integer c=1000;

System.out.println(a + "-" + b + "-" + a.getClass()+" c:"+c);   //2000-1000-class java.lang.Integer  c:1000

/*如果a=1,b=2,c=1则输出结果中的c会变成2,原因参考Interger缓存(-128到127),修改了缓存里的值导致1变成2了*/

}

static void swap(Integer a, Integer b) {
 int temp = a;

value.setAccessible(true);
value.setInt(a, b);
value.setInt(b, temp);
} catch (Exception ex) {
ex.printStackTrace();
}


}

 http://www.blogjava.net/sundc/archive/2013/06/08/400382.html

From:https://www.cnblogs.com/xuejianxiyang/p/9155397.html

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-01-20
  • 2022-01-11
  • 2021-09-21
  • 2022-12-23
  • 2022-03-10
  • 2022-12-23
猜你喜欢
  • 2022-02-24
  • 2022-12-23
  • 2022-12-23
  • 2021-06-11
  • 2021-10-03
  • 2021-07-23
相关资源
相似解决方案