wood burning stoves 2.0*
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 EJB 3 in Action this week in the EJB and other Java EE Technologies 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: 18113
    
  39

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)
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: == and != on wrappers
 
Similar Threads
query in answer
tricks to wrapper methods
Filters
why can we write List list = new ArrayList();
URLs and appending parameters