读书人

一个小技术细节有关问题

发布时间: 2011-12-06 23:05:08 作者: rapoo

一个小技术细节问题。
以下类为什么输出为true?

class c
{
public static void main(String[] args)
{

double a=0.5;
int b=1;
System.out.println(b==a+a);
}
}


[解决办法]
Quote from Java Language Specification:

"If the operands of an equality operator are both of numeric type, or one is of numeric type and the other is convertible (§5.1.8) to numeric type, binary numeric promotion is performed on the operands (§5.6.2). If the promoted type of the operands is int or long, then an integer equality test is performed; if the promoted type is float or double, then a floating-point equality test is performed. "

读书人网 >J2SE开发

热点推荐