aspose file tools*
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
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: 18981
    
  40

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)
 
 
subject: == and != on wrappers