在你看来,你遇到过的最令人惊讶、最怪异、最奇怪或最“WTF”的语言特性是什么?
请每个回答只回答一个特征。
在你看来,你遇到过的最令人惊讶、最怪异、最奇怪或最“WTF”的语言特性是什么?
请每个回答只回答一个特征。
当前回答
好吧,这也是我最喜欢的很难找到的虫子…将以0开头的整数视为八进制数。这导致了一个只会在早上8点到10点之间显示的错误:
有一次,我帮助构建了一个自动回归测试,在晚上通过cron执行。在一个20人的团队中,它几乎适用于每个人——除了一个开发人员偶尔会抱怨自动测试失败,但当手动运行时,一切都很好。这甚至一次都无法人工复制。
好吧,原因是,我们根据date命令的输出进行了一些统计计算(在bash中),这只在早上8:00到9:59期间失败,因为我们将小时值读取为“08”(这是一个非法的八进制值,而“01”-“07”是有效的八进制值,从“10”开始,所有内容都被再次视为十进制)……
其他回答
ActionScript 3:
当一个对象被它的接口使用时,编译器不识别从object继承的方法,因此:
IInterface interface = getInterface();
interface.toString();
给出一个编译错误。 解决方法是将类型转换为Object
Object(interface).toString();
PHP:
. 和+运算符。它有其合理的解释,但“a”+“5”= 5仍然显得尴尬。
Java(以及任何IEEE754的实现):
System.out.println(0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1);
输出0.9999999999999999
在x++ (Microsoft Dynamics AX):
1)需要在单独的行上使用分号(;)将变量声明与语句分开(至少在4.0版本之前)
int i;
int myArray[5];
;
i = 1;
2)数组索引是基于1的,所以你不允许像in那样使用索引0(零)从数组中读取
int myArray[5];
;
print myArray[0]; // runtime error
这并不奇怪,但是你可以在赋值的左边使用0索引,比如in
int myArray[5];
;
myArray[2] = 102;
myArray[0] = 100; // this is strange
print myArray[2]; // expcting 102?
会发生什么呢?数组被初始化为它的默认值,无论赋值中使用了什么值。上面的代码输出0(零)!
有趣的自动装箱和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充满了奇怪但整洁的特性。
If可以用在语句之前或之后,就像这样:
print "Hello World" if $a > 1;
if ($a > 1) { print "Hello World"; }
foreach也是如此:
print "Hello $_!\n" foreach qw(world Dolly nurse);
Python的三元操作符
在Python中,C三元操作符(c++示例:bool isNegative = i < 0 ?True: false;)可用作语法糖:
>>> i = 1
>>> "It is positive" if i >= 0 else "It is negative!"
'It is positive'
>>> i = -1
>>> "It is positive" if i >= 0 else "It is negative!"
'It is negative!'
这并不奇怪,而是一种特征。奇怪的是,与C中的顺序(条件?答:b)。