aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Wrappers Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Wrappers" Watch "Wrappers" New topic
Author

Wrappers

amarkirt saroay
Ranch Hand

Joined: Mar 16, 2008
Posts: 167


Which one is true?
i.equals(l)
l.equals(d)
d.equals(i)
i.equals(78)


SCJP-75%
SCWCD-82%
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41511
    
  53
That's rather easy to test, isn't it?


Ping & DNS - my free Android networking tools app
amarkirt saroay
Ranch Hand

Joined: Mar 16, 2008
Posts: 167
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

Joined: Jun 05, 2008
Posts: 110
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.


SCJP 5, SCWCD 5
Sunny Mattas
Ranch Hand

Joined: Apr 22, 2008
Posts: 45
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


Regards
Sunny Mattas
SCJP5
kaushik vira
Ranch Hand

Joined: Feb 01, 2007
Posts: 102
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 ]

kaushik Vira
-------------------------------------
SCJP, Preparing SCWCD..
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Wrappers