当前位置: 代码迷 >> J2SE >> 一个小技术细节有关问题
  详细解决方案

一个小技术细节有关问题

热度:159   发布时间:2016-04-24 15:43:14.0
一个小技术细节问题。
以下类为什么输出为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. "
  相关解决方案