• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Wrapper classes question

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,
I found this question in JQPlus:



Which of the following options are valid?
...
c) i.equals(d);
d) d.equals(ln);
e) ln.equals(42);



The answer pointed by the mock exam is that c) and d) are correct, and e) isn't ().
I looked at the forum archives and also tested the code myself, and it looks like the primitive 42 is converted to an Int when it's apssed as a parameter to the equals() method. So shouldn't the correct answers be: c), d) and e) (even though they all actually return false)?

Thanks,
Justyna W.
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The equals method in Long takes an Object, so before autoboxing was introduced in Java 1.5, passing a primitive int with ln.equals(42) would have been invalid. So my guess is this is just an old question.
 
Enthuware Software Support
Posts: 4832
52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are right, this has been fixed.
 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi friends,
If you look at the signature of the equals method in any of the wrapper classes it is like this...

public boolean equals(java.lang.Object);

In your case

c) i.equals(d);
d) d.equals(ln);
e) ln.equals(42);

options c and d are valid only because they are passing object. Where as in the option e '42' is not an object it is just an integer value. Hence the option e is not valid.
I think you are clear with this.

Thanks,
Ravindranath
 
Ranch Hand
Posts: 381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ravindra option ( e ) is not only valid it compiles and runs too.
Any explantaion?
 
Ravindranath Chowdary
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First of all I want to know on which version of J2SE you wrote the mock exam. From the key as you said that the c and d are correct options and e is only valid from J2SE5.0.

The reply I have posted earlier is the one that i have worked in the J2SE version 1.4.2_05. But I can tell one thing that in Java Tiger (i.e J2SE 5.0) there is a concept of AutoBoxing( Boxing + Unboxing).

In this
Boxing:- Integer iWrapper = 10; compiles fine
UnBoxing:- int iPrimitive = iwrapper; compiles fine

I think that in your case the value 42 converts into corresponding Wrapper class object and compiles fine.

Thanks,
Ravindranath.
reply
    Bookmark Topic Watch Topic
  • New Topic