10.12.2012 Views

The Java Language Specification, Third Edition

The Java Language Specification, Third Edition

The Java Language Specification, Third Edition

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

EXPRESSIONS Reference Equality Operators == and != 15.21.3<br />

• <strong>The</strong> value produced by the == operator is true if the value of the left-hand<br />

operand is equal to the value of the right-hand operand; otherwise, the result is<br />

false.<br />

• <strong>The</strong> value produced by the != operator is true if the value of the left-hand<br />

operand is not equal to the value of the right-hand operand; otherwise, the<br />

result is false.<br />

15.21.2 Boolean Equality Operators == and !=<br />

If the operands of an equality operator are both of type boolean, or if one operand<br />

is of type boolean and the other is of type Boolean, then the operation is boolean<br />

equality. <strong>The</strong> boolean equality operators are associative.<br />

If one of the operands is of type Boolean it is subjected to unboxing conversion<br />

(§5.1.8).<br />

<strong>The</strong> result of == is true if the operands (after any required unboxing conversion)<br />

are both true or both false; otherwise, the result is false.<br />

<strong>The</strong> result of != is false if the operands are both true or both false; otherwise,<br />

the result is true. Thus != behaves the same as ^ (§15.22.2) when applied<br />

to boolean operands.<br />

15.21.3 Reference Equality Operators == and !=<br />

Things are more like they are now than they ever were before.<br />

—Dwight D. Eisenhower<br />

If the operands of an equality operator are both of either reference type or the null<br />

type, then the operation is object equality.<br />

A compile-time error occurs if it is impossible to convert the type of either<br />

operand to the type of the other by a casting conversion (§5.5). <strong>The</strong> run-time values<br />

of the two operands would necessarily be unequal.<br />

At run time, the result of == is true if the operand values are both null or<br />

both refer to the same object or array; otherwise, the result is false.<br />

<strong>The</strong> result of != is false if the operand values are both null or both refer to<br />

the same object or array; otherwise, the result is true.<br />

While == may be used to compare references of type String, such an equality<br />

test determines whether or not the two operands refer to the same String<br />

object. <strong>The</strong> result is false if the operands are distinct String objects, even if<br />

they contain the same sequence of characters. <strong>The</strong> contents of two strings s and t<br />

can be tested for equality by the method invocation s.equals(t). See also<br />

§3.10.5.<br />

DRAFT<br />

507

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!