在你看来,你遇到过的最令人惊讶、最怪异、最奇怪或最“WTF”的语言特性是什么?
请每个回答只回答一个特征。
在你看来,你遇到过的最令人惊讶、最怪异、最奇怪或最“WTF”的语言特性是什么?
请每个回答只回答一个特征。
当前回答
在C或c++中,sizeof…参数的圆括号是可选的。假设参数不是类型:
void foo() {
int int_inst;
// usual style - brackets ...
size_t a = sizeof(int);
size_t b = sizeof(int_inst);
size_t c = sizeof(99);
// but ...
size_t d = sizeof int_inst; // this is ok
// size_t e = sizeof int; // this is NOT ok
size_t f = sizeof 99; // this is also ok
}
我一直不明白这是为什么!
其他回答
有趣的自动装箱和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;
Perl:
可以编写一个完全由标点符号组成的程序。
这是怎么回事?!
在PHP中,你可以这样做:
System.out.print("hello");
NSIS (Nullsoft Scriptable安装系统)有StrCmp指令:
StrCmp str1 str2 jump_if_equal [jump_if_not_equal] 比较str1和str2(不区分大小写)。如果str1和str2相等,则Gotos jump_if_equal,否则Gotos jump_if_not_equal。 StrCmp $0“一个字符串”0 +3 DetailPrint '$$0 == "a string"' 转到+ 2 DetailPrint '$$0 != "a string"'
锦上添花:jump_if_equal和jump_if_not_equal也可以是负数。但我猜你们已经从正数前面的+符号中算出来了。我不记得这是强制性的,还是只是一个可怕的惯例。
这基本上结合了最差的BASIC和最差的Assembler。
在Perl中(没有“使用严格”或“使用警告”):
if(true==undef)
{
print "True\n";
}
else{
print "False\n";
}
if(undef)
{
print "True\n";
}
else{
print "False\n";
}
if(true)
{
print "True\n";
}
else{
print "False\n";
}
打印:
True
False
True