This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

About wrap class and ==,equals;

 
Ronyn Woods
Greenhorn
Posts: 27
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could you tell me the answers and why? thanks a lot.
Integer i = new Integer("42");
Long l = new Long("42L");
Double d = new Double("42.0");
Which returns true ?(choose one or more)
a)i==d;
b)i==l;
c)l==d;
d)i.equals(d);
e)d.equals(i);
f)i.equals("42");
 
Mikael Jonasson
Ranch Hand
Posts: 158
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I believe that only f is coorect here. The wraperclasses doesn't have converters.
The == operator isn't defined for the wrapperclasses, so it tries to compare the objects themselves, which will result in conversion error.
/Mike
 
Ravindra Mohan
Ranch Hand
Posts: 216
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Folks,
The answer is none of these. You can compile the code
and check the same. If you see the API , then you can see that
each Wrapper class overrides the equals() of the Object and returns "true" if and only if the argument is not null and is a corresponding wrapper class object that represents the same wrapper class object value as the object being compared.
for example lets take a boolean primitive and its corresponding
wrapper class Boolean:

In case you want to compare the primitive values and the corresponding wrapper class object's value , then you should
use the typeValue() of the corresponding wrapper class.
Lets take the boolean as the example, viz., usage of booleaValue() method of Boolean Class :

Hope your doubts are cleared now.
Ravindra Mohan.

[This message has been edited by Ravindra Mohan (edited May 22, 2001).]
 
Susan Delph
Ranch Hand
Posts: 34
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wrote a short program to test these. Here are the results.
Options a,b,c all give compiler errors:

I got rid of these lines. The program then compiled OK, but gave me NumberFormatExeception on the line:
Long l = new Long("42L");
So I changed this line to read:
Long l = new Long("42");
The program then compiled and ran. The remaining tests all gave 'false' results:
d) i.equals(d);
e) d.equals(i);
f) i.equals("42");
Ravindra's message explained why these gave 'false' results.
Susan

[This message has been edited by Susan Delph (edited May 22, 2001).]
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic