• 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 all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
  • Piet Souris
  • Frits Walraven
  • Carey Brown

About wrap class and ==,equals;

Posts: 27
  • 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)
Ranch Hand
Posts: 158
  • 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.
Ranch Hand
Posts: 216
  • 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).]
Ranch Hand
Posts: 34
  • 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.

[This message has been edited by Susan Delph (edited May 22, 2001).]
Honk if you love justice! And honk twice for tiny ads!
Thread Boost feature
    Bookmark Topic Watch Topic
  • New Topic