This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes AutoBoxing question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "AutoBoxing question" Watch "AutoBoxing question" New topic
Author

AutoBoxing question

William Yan
Ranch Hand

Joined: Sep 26, 2006
Posts: 69

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


Java is better and better, yet bitter and bitter.
wise owen
Ranch Hand

Joined: Feb 02, 2006
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

Joined: Oct 24, 2006
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

Joined: Feb 07, 2005
Posts: 2367
The new operator always creates a new object.
Bing Qiao
Greenhorn

Joined: Oct 24, 2006
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

Joined: Feb 07, 2005
Posts: 2367
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

Joined: Sep 25, 2006
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

Joined: Oct 24, 2006
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

Joined: Sep 26, 2006
Posts: 69
IC.
Thanks alot.
 
GeeCON Prague 2014
 
subject: AutoBoxing question