When can I use “==” operator?

99封情书 提交于 2019-12-30 13:39:02

问题


I have found quote from jls:

The equality operators may be used to compare two operands that are convertible (§5.1.8) to numeric type, or two operands of type boolean or Boolean, or two operands that are each of either reference type or the null type. All other cases result in a compile-time error.

But this code

   String str= "";
   Number num = 1;
   System.out.println(str == num);

every operand is reference!

said that it is incompatible types.

Where did in jls say that these types should be compatible ?


回答1:


In section 15.21.3 (Reference Equality Operators == and !=):

It is a compile-time error if it is impossible to convert the type of either operand to the type of the other by a casting conversion (§5.5). The run-time values of the two operands would necessarily be unequal.



来源:https://stackoverflow.com/questions/23805024/when-can-i-use-operator

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!