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");
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
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 nulland 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).]
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).]
Honk if you love justice! And honk twice for tiny ads!