1

我在这个问题上的第一次尝试是在 C 中

#define 2 5
assert(2+2 == 10);

很遗憾

error: macro name must be an identifier

我也试过方案

(define 2 5)

can't define a non-symbol: (define 2 5)

我想知道是否有任何编程语言可以做到这一点。

4

1 回答 1

3

我真诚地希望不会。

但是,我确实知道 Java 中的一种方法:您可以使用反射来调整缓存的装箱Integers 的值:JVM 必须缓存 -128 到 +127 范围内的所有值,并且确实存在一种调整机制该缓存中的数值!

有关更多详细信息,请参阅https://codegolf.stackexchange.com/questions/28786/write-a-program-that-makes-2-2-5/28818#28818。这是完整的代码:

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,本质上是通过重新定义数字 4 来实现的。

于 2016-06-15T14:59:26.383 回答