简体   繁体   中英

When can I use “==” operator?

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 ?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM