首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

一个小技术细节有关问题

2011-12-06 
一个小技术细节问题。以下类为什么输出为true?classc{publicstaticvoidmain(String[]args){doublea0.5int

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

热点排行