programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Jeanne Boyarsky
• Ron McLeod
• Paul Clapham
• Liutauras Vilda
Sheriffs:
• paul wheaton
• Rob Spoor
• Devaka Cooray
Saloon Keepers:
• Stephan van Hulst
• Tim Holloway
• Carey Brown
• Frits Walraven
• Tim Moores
Bartenders:
• Mikalai Zaikin

# AutoBoxing question

Ranch Hand
Posts: 69
• Number of slices to send:
Optional 'thank-you' note:

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

Ranch Hand
Posts: 2023
• Number of slices to send:
Optional 'thank-you' note:
15.21.2 Boolean Equality Operators == and !=

15.21.3 Reference Equality Operators == and !=

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

Greenhorn
Posts: 21
• Number of slices to send:
Optional 'thank-you' note:

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!

Ranch Hand
Posts: 2412
• Number of slices to send:
Optional 'thank-you' note:
The new operator always creates a new object.

Bing Qiao
Greenhorn
Posts: 21
• Number of slices to send:
Optional 'thank-you' note:
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: 2412
• Number of slices to send:
Optional 'thank-you' note:
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 ]

Greenhorn
Posts: 11
• Number of slices to send:
Optional 'thank-you' note:

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
• Number of slices to send:
Optional 'thank-you' note:

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
• Number of slices to send:
Optional 'thank-you' note:
IC.
Thanks alot.

 I love a good mentalist. And so does this tiny ad: a bit of art, as a gift, that will fit in a stocking https://gardener-gift.com
reply
Similar Threads