网上看到的一句不错的打油诗:锄禾日当午,不如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++|有感 Visual Studio 2015 RTM 简介 - 八年后回归 Dot Net,终于迎来了 Mvc 时代,盼走了 Web 窗体时代...
- C#|C# 文件路径操作
- C# 接口实例
- 个人日记|K8s中Pod生命周期和重启策略
- C/C++|C/C++ basis 02
- 学习分享|【C语言函数基础】
- C++|C++浇水装置问题
- C#|10、接口、抽象、密封、开放封闭原则
- c#|11、C#处理程序异常的技术
- C#|九、C#结构 类 属性