Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
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 Building Microservices this week in the Design 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: 20057
    
  44

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)
 
Have you checked out Aspose?
 
subject: == and != on wrappers
 
It's not a secret anymore!