This week's book giveaway is in the OCAJP forum.
We're giving away four copies of OCA Java SE 8 Programmer I Study Guide 1Z0-808 and have Jeanne Boyarsky & Scott Selikoff on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes == and != on wrappers Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA Java SE 8 Programmer I Study Guide 1Z0-808 this week in the OCAJP forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "== and != on wrappers" Watch "== and != on wrappers" New topic
Author

== and != on wrappers

Mauricio Bonetti
Greenhorn

Joined: Aug 25, 2008
Posts: 4
Hi,

Solving some questions about wrappers I got really in doubt on how to use == and != operators.

I've read K&B 5 chapter about wrappers but the portugueese translation drove me nuts !

What do they compare ? They look if the reference instances the same object ?? Is that ??
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19235
    
  41

Simply....

If both sides are wrappers (meaning instances), then it is comparing the references. The two sides are equal if they are the same object.

If only one side is a wrapper, then autoboxing will unbox the wrapper -- and it is the values that will be compared. The two sides are equal if they represent the same value.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
 
wood burning stoves
 
subject: == and != on wrappers