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不完全面向对象的一个小问题吧!
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值