技术头条 - 一个快速在微博传播文章的方式     搜索本站
您现在的位置首页 --> 算法 --> 如何编程实现 2 + 2 = 5?

如何编程实现 2 + 2 = 5?

浏览:5200次  出处信息

   Write a program that makes 2 + 2 = 5,看到这个题目,感觉很新颖,第一个答案就是用Java实现的。用上了Java中的整型实例池的概念。以前只看到过实例池导致两个对象的指针相同的问题,即

Integer a = new Integer(2);

   Integer b = new Integer(2);

   System.out.print(a == b);

   

   

   上面的代码最终输出的是true,按照Java对象的申请原则来说,这里应该是false才对。正是因为JVM在实现的时候,默认生成了一些Integer对象的实例,当需要的实例是池子中已经存在的数值时,直接返回已经生成的对象的引用,不必新构造对象。这样可以极大减少实例数目和程序运行性能。

   

   而这个题目是将池子中的对象的内容进行了修改,最终使得取回的实例的值发生了改变。这样其实很危险的,如果在正式运行程序的业务代码之前,做这个修改,那么整个程序的运行逻辑将产生混乱。

import java.lang.reflect.Field;


   public class Main {

       public static void main(String[] args) throws Exception {

           Class cache = Integer.class.getDeclaredClasses()[0];

           Field c = cache.getDeclaredField("cache");

           c.setAccessible(true);

           Integer[] array = (Integer[]) c.get(cache);

           array[132] = array[133];

           System.out.printf("%d",2 + 2);

       }

   }

   

   上面是具体的代码,最终输出的结果为5,作者给出的解释为:

   You need to change it even deeper than you can typically access. Note that this is designed for Java 6 with no funky parameters passed in on the JVM that would otherwise change the IntegerCache.

   Deep within the Integer class is a Flyweight of Integers. This is an array of Integers from −128 to +127. cache[132] is the spot where 4 would normally be. Set it to 5.

   利用缓存的读写接口,将4这个实例的缓存对象的指针改为指向5的实例对象了,这样,当应用程序取出4时,实际上返回的是5的引用,打印出来的也就是5了。



QQ技术交流群:445447336,欢迎加入!
扫一扫订阅我的微信号:IT技术博客大学习
© 2009 - 2024 by blogread.cn 微博:@IT技术博客大学习

京ICP备15002552号-1