This week's book giveaway is in the Big Data forum.
We're giving away four copies of Elasticsearch in Action and have Radu Gheorghe & Matthew Lee Hinman 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 Elasticsearch in Action this week in the Big Data 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: 19345
    
  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)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: == and != on wrappers