gstackoverflow
gstackoverflow

Reputation: 37034

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 ?

Upvotes: 0

Views: 132

Answers (1)

Jon Skeet
Jon Skeet

Reputation: 1500465

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.

Upvotes: 5

Related Questions