Numerical Comparison

网上看到的一句不错的打油诗:锄禾日当午,不如coding苦,对着C++,一调一下午。

以下内容摘自Java Language Specificaton 3rd:503页:
Numerical Comparison Operators <,<=,>,and >=

The type of each of the operands of a numerical comparison operator must be a type that is convertible to a primitive numeric type, or a compile time error occurs. Binary numeric promotion is performed on the operands. If th promoted type of the operands is int or long, then signed integer comparison is performed; if this promoted type is float or double, then floating-point comparison is performed.
Note that binary numeric promotion performs value set conversion and unboxing conversion. Comparison is carried out accurately on floating-point values, no matter what value sets their representing values were draw from.
注:Numeric Promotion分为:Unary Numeric Promotion和Binary Numeric Promotion。两种不同的数值转换类型分别说明了在特定的操作符中数字类型的转换规则。
Java中对于比较运算符:<,<=,>和>=会进行Binary Numeric Promotion,而BNP实际上包括了value set的转换和unboxing。当我们用诸如Integer,Float对象跟常量值进行比较的时候一定要记得判空。如:if(a>1){} 若a为null,就会有空指针异常。所以有时候用Primitive类型更加直接方便.这也是Java不完全面向对象的一个小问题吧!

你可能感兴趣的:(C,C++,C#)