This week's book giveaway is in the Design forum.We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!

# AutoBoxing question

William Yan
Ranch Hand
Posts: 69

output: f t t f.
WHY? Thanks a lot.

wise owen
Ranch Hand
Posts: 2023
15.21.2 Boolean Equality Operators == and !=

15.21.3 Reference Equality Operators == and !=

[ October 25, 2006: Message edited by: wise owen ]

Bing Qiao
Greenhorn
Posts: 21
Originally posted by wise owen:
15.21.3 Reference Equality Operators == and !=

[ October 25, 2006: Message edited by: wise owen ]

"The run-time values of the two operands would necessarily be unequal."
What exactly does this mean?

Many thanks!

Keith Lynn
Ranch Hand
Posts: 2409
The new operator always creates a new object.

Bing Qiao
Greenhorn
Posts: 21
Thanks!

Right. But given the next sentence: "At run time, the result of == is true if the operand values are both null or both refer to the same object or array; otherwise, the result is false."

It's very much possible that "the run-time values of the two operands are actually equal because they refer to the same object!"

Saying "would necessarily be unequal" without giving a proper context only makes people confused, doesn't it? Or it's my English not good enough to understand this kind of subtlety?

Keith Lynn
Ranch Hand
Posts: 2409
The sentence you refer to is not a general statement. It is part of this paragraph.

A compile-time error occurs 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.

This means that the == operator cannot be used between references where one is not a subtype of the other.

This means, for example, that you cannot do something like this.

Since neither FileReader nor String is a subtype of the other, there is no chance that the objects file and s refer to could be the same.
[ October 25, 2006: Message edited by: Keith Lynn ]

anvesh charuvaka
Greenhorn
Posts: 11
Originally posted by William Yan:

output: f t t f.
WHY? Thanks a lot.

in the first case you are comparing the reference variables of two different Objects so the expression evaluates to false

in the second and third cases since b3 is a primitive, b1 and b4 are unboxed and their primitive values are returned for comparision so these expressions evaluate to true.

in the third case the same logic applies as the first because b1 and b4 are different objects on the heap.

But if you had another object
Boolean b5=true;
then
b4==b5 will evaluate to true because the jvm assigns the same object to bothe the references b4 and b5.(something like Strings literals in String Pool)

Bing Qiao
Greenhorn
Posts: 21
But if you had another object
Boolean b5=true;
then
b4==b5 will evaluate to true because the jvm assigns the same object to bothe the references b4 and b5.(something like Strings literals in String Pool)

Good reminder!

William Yan
Ranch Hand
Posts: 69
IC.
Thanks alot.