• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

equals and == with wrappers

 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
See where your hand is? Not there. It's next to this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic