[WTF ] 2 + 2 = 5
在 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://www.ptt.cc/bbs/java/M.1401738895.A.4DC.html
推
06/03 04:10, , 1F
06/03 04:10, 1F
推
06/03 06:43, , 2F
06/03 06:43, 2F
→
06/03 09:49, , 3F
06/03 09:49, 3F
推
06/03 09:52, , 4F
06/03 09:52, 4F
→
06/03 10:12, , 5F
06/03 10:12, 5F
推
06/03 10:48, , 6F
06/03 10:48, 6F
推
06/03 10:52, , 7F
06/03 10:52, 7F
→
06/03 11:50, , 8F
06/03 11:50, 8F
→
06/03 11:59, , 9F
06/03 11:59, 9F
→
06/03 12:00, , 10F
06/03 12:00, 10F
在古老的歲月裡, final field 曾經不是那麼 final 的 :D
│ 文章代碼(AID): #1Bsfo3Kx (java) [ptt.cc] Re: [閒聊] 魔法 JNI │
│ 文章網址: http://www.ptt.cc/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
→
06/03 13:53, , 11F
06/03 13:53, 11F
討論串 (同標題文章)
以下文章回應了本文:
完整討論串 (本文為第 1 之 3 篇):