在你看来,你遇到过的最令人惊讶、最怪异、最奇怪或最“WTF”的语言特性是什么?
请每个回答只回答一个特征。
在你看来,你遇到过的最令人惊讶、最怪异、最奇怪或最“WTF”的语言特性是什么?
请每个回答只回答一个特征。
当前回答
Perl:
可以编写一个完全由标点符号组成的程序。
这是怎么回事?!
其他回答
有趣的自动装箱和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;
在c++中,你可以从空指针调用静态方法——看!
class Foo {
public:
static void bar() {
std::cout << "WTF!?" << std::endl;
}
};
int main(void) {
Foo * foo = NULL;
foo->bar(); //=> WTF!?
return 0; // Ok!
}
这句话让我很意外……
在J中,foreign(!:)是各种函数组合在一起。左边的参数是一个类别,右边的参数通常(但不总是)是不同的增量值。的东西。例如:
2!:55 NB. Close console 9!:10 NB. Set print precision 6!:0 NB. Actual time 6!:2 NB. Execution time 4!:3 NB. Loaded scripts
当然,聪明的做法是把它们包装起来,但有些你只需要记住。顺便说一句,所有这些都是,想想看,三位一体的,有两个参数在右边,一个在左边。除非你给他们一个最终有效的论据,否则以上任何一条都不会起作用。
在PHP中,字符串和函数指针一样好:
$x = "foo";
function foo(){ echo "wtf"; }
$x(); # "wtf"
不幸的是,这并不管用:
"foo"();
Javascript中有很多东西会让你流泪。
局部变量的作用域,举个简单的例子:
function foo(obj)
{
for (var n = 0; n < 10; n++)
{
var t; // Here is a 't'
...
}
t = "okay"; // And here's the same 't'
}