• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Scott Selikoff
Bartenders:
  • Piet Souris
  • Jj Roberts
  • fred rosenberger

About wrap class and ==,equals;

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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");
 
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 216
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).]
 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).]
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic