在你看来,你遇到过的最令人惊讶、最怪异、最奇怪或最“WTF”的语言特性是什么?
请每个回答只回答一个特征。
在你看来,你遇到过的最令人惊讶、最怪异、最奇怪或最“WTF”的语言特性是什么?
请每个回答只回答一个特征。
当前回答
有趣的自动装箱和Java中的整数缓存:
Integer foo = 1000;
Integer bar = 1000;
foo <= bar; // true
foo >= bar; // true
foo == bar; // false
//However, if the values of foo and bar are between 127 and -128 (inclusive)
//the behaviour changes:
Integer foo = 42;
Integer bar = 42;
foo <= bar; // true
foo >= bar; // true
foo == bar; // true
解释
快速浏览一下Java源代码将会出现以下内容:
/**
* Returns a <tt>Integer</tt> instance representing the specified
* <tt>int</tt> value.
* If a new <tt>Integer</tt> instance is not required, this method
* should generally be used in preference to the constructor
* {@link #Integer(int)}, as this method is likely to yield
* significantly better space and time performance by caching
* frequently requested values.
*
* @param i an <code>int</code> value.
* @return a <tt>Integer</tt> instance representing <tt>i</tt>.
* @since 1.5
*/
public static Integer valueOf(int i) {
if (i >= -128 && i <= IntegerCache.high)
return IntegerCache.cache[i + 128];
else
return new Integer(i);
}
注意:IntegerCache。High默认为127,除非由属性设置。
自动装箱的情况是,foo和bar都是从缓存中检索到相同的整数对象,除非显式创建:例如,foo = new integer(42),因此在比较引用是否相等时,它们将为真而不是假。比较Integer值的正确方法是使用.equals;
其他回答
对于那些不知道的人来说,PostScript实际上是一种编程语言。我已经有点疯狂了——我写了一个PostScript程序,计算曼德尔布罗特分形的细节非常高。它真的是可打印的PostScript,尽管它会崩溃很多打印驱动程序…
总之,从哪里开始PostScript…这里有一个例子:您实际上可以创建一个标识符为....的变量什么都没有。
() cvn 5 def %将数字5分配给…没有什么
PostScript是一种基于堆栈的语言。()将一个空字符串放入堆栈。cvn将其转换为一个名称(如果打印它,则为“/”,因为PS中的所有名称前面都有斜杠)。然后5def将值5赋给它。(%是注释字符)
你不能直接把它拿回来,例如,如果我说“/ print”,这将不会打印数字5。但你可以间接地把它拿回来:
() CVN负载打印%这将打印数字5
还有什么……PostScript将字典作为原生类型,您可以使用数组引用作为字典的键…但是REFERENCE才是键,而不是数组。所以:
/myDict 100 dict def
[0] dup myDict exch 42 put myDict exch get == %打印42
myDict [1] 42 put myDict [1] get %抛出未定义错误
编辑:哦,是的,还有一件有趣的事情……在Ghostscript提示下尝试以下操作:
1 array dup dup 0 exch put ==
分析!
在Java中从文本文件中读取一行。
BufferedReader in = null;
try {
in = new BufferedReader(new FileReader("filename"));
String str;
str = in.readLine();
if (str != null) {
...
}
} catch (IOException e) {
...
} finally {
try {
if (in != null) {
in.close();
}
} catch (IOException e) {}
}
啊。虽然我承认这并不奇怪……只是邪恶。: -)
更短、更习惯的版本:
try {
BufferedReader in = new BufferedReader(new FileReader("filename"));
try {
String str = in.readLine();
while (str != null) {
str = in.readLine();
}
} finally {
in.close();
}
} catch (IOException e) {
e.printStackTrace();
}
在JavaScript中:
'5' + 3 gives '53'
而
'5' - 3 gives 2
我曾经写过一种编程语言,它有一个“strfry”操作符:
"hello world"?
# => "wdo rlholle"
有用的,是吗?
在一个奇怪的银行系统中使用RSL编程语言。数组有内置类TArray。但如果你从它继承,每个实例变量都成为数组的一个元素。
class (TArray) DerivedArray
var someField = 56;
end
var a = DerivedArray();
PrintLn(a.Size); // => 1
PrintLn(a[0]); // => 56