• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Wrappers

 
amarkirt saroay
Ranch Hand
Posts: 167
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Which one is true?
i.equals(l)
l.equals(d)
d.equals(i)
i.equals(78)
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • 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
  • 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?
 
Antonio Tercero
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
  • 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.
 
Sunny Mattas
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • 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
 
kaushik vira
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • 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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic