日期:2014-05-20  浏览次数:20618 次

一个小技术细节问题。
以下类为什么输出为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. "