Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

== and != on wrappers

 
Mauricio Bonetti
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21218
81
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic