在路上

 找回密码
 立即注册
在路上 站点首页 学习 查看内容

java误用反射会带来的一个小问题,2+2=5!

2016-12-20 13:11| 发布者: zhangjf| 查看: 426| 评论: 0

摘要: public class TestInteger { public static void main(String args) throws NoSuchFieldException,IllegalAccessException { Class cache = Integer.class.getDeclaredClasses();//1 Field myCache = cache. ...

public class TestInteger {

  1. public static void main(String[] args) throws NoSuchFieldException,IllegalAccessException {
  2. Class cache = Integer.class.getDeclaredClasses()[0];//1
  3. Field myCache = cache.getDeclaredField("cache");//2
  4. myCache.setAccessible(true);//3
  5. Integer[] newCache = (Integer[]) myCache.get(cache);//4
  6. newCache[132] = newCache[133];//5
  7. int a = 2;
  8. int b = a+a ;
  9. System.out.printf("%d+%d=%d",a,a,b);
  10. }
复制代码

}

最新评论

小黑屋|在路上 ( 蜀ICP备15035742号-1 

;

GMT+8, 2025-7-7 21:33

Copyright 2015-2025 djqfx

返回顶部