java5>>2_<转>如何编程实现 2 + 2 = 5?

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

2014/10/16 12:49:55  yingbin920  程序员俱乐部  我要评论(0)

摘要:原链接:http://codeway.co/%E5%A6%82%E4%BD%95%E7%BC%96%E7%A8%8B%E5%AE%9E%E7%8E%B0-2-2-5%EF%BC%9F/Writeaprogramthatmakes2+2=5,看到这个题目,感觉很新颖,第一个答案就是用Java实现的。用上了Java中的整型实例池的概念。以前只看到过实例池导致两个对象的指针相同的问题,即Integera=newInteger(2);Integerb=newInteger(2);System.out

原链接:http://codeway.co/%E5%A6%82%E4%BD%95%E7%BC%96%E7%A8%8B%E5%AE%9E%E7%8E%B0-2-2-5%EF%BC%9F/

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);

a = 2;

b = 2;

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

上面代码中第一处输出的是false,按照Java对象的申请原则来说,new出来的对象地址是不一样的。而第二处因为使用了缓存,因此输出的是true。正是因为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了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值