作者AmosYang (Zzz...)
看板java
标题[WTF ] 2 + 2 = 5
时间Tue Jun 3 03:54:51 2014
在 Hacker News 上看到这个讨论串
https://news.ycombinator.com/item?id=7828757
题目很简单: 写个程式「看起来」是在计算 2+2 但最後输出 "2+2" 的答案为 5
"2 + 2 = 5" 这个梗来自於 George Orwell 所着 1984 这本小说
以下程式码节选自网友参赛作品
============================================================
By http://codegolf.stackexchange.com/users/17249/durron597
============================================================
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);
}
}
============================================================
============================================================
By http://codegolf.stackexchange.com/users/17249/durron597
============================================================
public class TwoPlusTwo {
public static void main(String... args) {
double two = two();
System.out.format("Variable two = %.15f%n", two);
double four = Math.ceil(two + two); // round just in case
System.out.format("two + two = %.15f%n", four);
}
// 20 * .1 = 2
private static double two() {
double two = 0;
for(int i = 0; i < 20; i++) {
two += .1;
}
return two;
}
}
============================================================
============================================================
Originally written in C# by http://codegolf.stackexchange.com/users/20329/grax
============================================================
public class HelloWorld{
public static void main(String []args){
int x = 2;
int y = 2;
if (false);
{
++x;
}
System.out.println(x+y);
}
}
============================================================
============================================================
By http://codegolf.stackexchange.com/users/7416/aditsu
============================================================
public class Five {
public static void main(final String... args) {
System.out.println(256.0000000000002 + 256.0000000000002);
}
}
============================================================
同场加映,猜猜看以下这个程式的 output
============================================================
By https://news.ycombinator.com/user?id=kyrra
============================================================
public class A {
public static void main(String[] args) {
Integer a = 100, b = 100;
Integer c = 10000, d = 10000;
System.out.println(a == b);
System.out.println(c == d);
}
}
============================================================
该把 "Can understand Java code" 这行从我的履历表上拿掉了… :D
--
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 75.177.186.231
※ 文章网址: http://webptt.com/cn.aspx?n=bbs/java/M.1401738895.A.4DC.html
3F:→ sbrhsieh:作法一可行也是因为5落在有cache范围内 06/03 09:49
4F:推 Killercat:同场加映那个 其实算是FAQ等级的陷阱了 XD 06/03 09:52
5F:→ sbrhsieh:我打错了,是4 06/03 10:12
6F:推 darkk6:不过同理,可以扩增IntegerCache的范围,要多少都不是问题 06/03 10:48
7F:推 Killercat:auto boxing其实最容易出包的地方是method =P 06/03 10:52
8F:→ sbrhsieh:不懂darkk6 的意思 06/03 11:50
9F:→ sbrhsieh:若我没误解,那你有能力变更 final field 的值。 06/03 11:59
10F:→ sbrhsieh: 要 06/03 12:00
在古老的岁月里, final field 曾经不是那麽 final 的 :D
│ 文章代码(AID):
#1Bsfo3Kx (java) [ptt.cc] Re: [闲聊] 魔法 JNI │
│ 文章网址:
http://webptt.com/cn.aspx?n=bbs/java/M.1272618115.A.53B.html │
JustinHere 的文章里有点出另一个方法 (我没亲身试过 :> 有问题找他)
http://openhome.cc/Gossip/Java/AutoBoxUnBox.html
IntegerCache.low预设值是-128,执行时期无法更改,IntegerCache.high
预设值是127,可以於启动JVM时,使用系统属性java.lang.Integer.IntegerCache.high
来指定。例如:
> java -Djava.lang.Integer.IntegerCache.high=300 cc.openhome.Demo
※ 编辑: AmosYang (75.177.186.231), 06/03/2014 12:37:12
11F:→ sbrhsieh:这个 property 应该也是 implementation specific? 06/03 13:53