Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

equals and == with wrappers

 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


My question is, under what case we will get true while comparing two wrappers (ofcourse same), or one wrapper and one primitive.


Byte bb1 = 100; //Why isn't it giving error like, can't convert from int to Byte? While Line 1 and Line 2 are giving the same!!! :roll:


Thanks,
cmbhatt
[ April 15, 2007: Message edited by: Chandra Bhatt ]
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See this topic in our SCJP FAQ.
[ April 15, 2007: Message edited by: marc weber ]
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Marc,

But specifically my Question is regarding comparison between one primitive and
wrapper and using equals to compare two different wrappers.
How equals comparison is done?




Thanks,
cmbhatt
[ April 16, 2007: Message edited by: Chandra Bhatt ]
 
Omer Haderi
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi there,

Byte bb1 = 100; works fine because 100 is in the range of the byte -128 to 127 if you change the value to 128 then you will get the same error.

As you already may know the signature of equals is


so you can pass any object there but the result will be false, because before equality the object is checked with the instanceof operator.

check specific implementations:


As of java 5 the primitives are autoboxed to Wrapper classes, so they are becoming objects.

Cheers,
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Omer,


Thanks for giving definition of the equals() for each wrapper. I was under impression that equals will return true if values are equal but I forgot the
"vital" instanceof test before bitwise comparison.


Thanks again,
cmbhatt
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic