jQuery in Action, 3rd edition
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes About wrap class and ==,equals; Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "About wrap class and ==,equals;" Watch "About wrap class and ==,equals;" New topic

About wrap class and ==,equals;

Ronyn Woods

Joined: May 16, 2001
Posts: 27
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)
Mikael Jonasson
Ranch Hand

Joined: May 16, 2001
Posts: 158
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.
Ravindra Mohan
Ranch Hand

Joined: Mar 16, 2001
Posts: 216
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

Joined: Feb 24, 2001
Posts: 34
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).]
I agree. Here's the link: http://aspose.com/file-tools
subject: About wrap class and ==,equals;
It's not a secret anymore!