• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Wrappers

 
Ranch Hand
Posts: 167
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Which one is true?
i.equals(l)
l.equals(d)
d.equals(i)
i.equals(78)
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's rather easy to test, isn't it?
 
amarkirt saroay
Ranch Hand
Posts: 167
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I have checked it but whats the explanation? Why does comparison with primitive return true and ok wrappers are incompatible to compare.



Originally posted by Ulf Dittmer:
That's rather easy to test, isn't it?

 
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's because wrappers don't pass the "IS-test":
Integer IS NOT a Double, Integer IS NOT a Double, etc ..
Check the inheritance tree for wrappers.
 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi




Also i think equals() method in these wrapper classes is using instance of operator to check the equality. since wrapper classes are peers i.e. none of them extend each other they will always give false for instance of test.
Thus


now for



78 will be auto boxed into Integer class and wiil pass instance of test.since both is having 78 as its value will return true.

Regards
 
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is pretty much clear. if you seen the equals method implementation in Integer class.

public boolean equals(Object obj) {
if (obj instanceof Integer) {
return value == ((Integer)obj).intValue();
}
return false;
}

if first applies instance off test and if it success then latter it will check value.
[ July 22, 2008: Message edited by: kaushik vira ]
reply
    Bookmark Topic Watch Topic
  • New Topic