• 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

Question About equals( ) method

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Given the following Code:

Integer i = new Integer(42);
Long l = new Long (42);
Double d = new Double (42.0);

Which of the following two expression evaluvate to True?

A. (i == l)
B. (i == d)
C. (d == l)
D. (i.equals(d))
E. (d.equals(i))
F. (i.equals(42))

As far as my understand of equals method is that in wrapper class equals ()
method only return true if both the primitive values and wrapper's classes are the same.

I have tried to run this code in the but all of them return false.
 
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
F
 
Ranch Hand
Posts: 168
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The answers A, B, and c will return false because the objects i, l, and d are different objects.

The answers D and E will return false because the paramater's class type is not the same as the objects where the methods belong.

The answer F will not even compile.
 
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A, B and C will generate compilation error because of incomparable type. D and E will return false because of type difference. As for F, the outcome depends on which JDK version you're running. Returns true if using JDK 5.0 due to the autoboxing feature, but gives compilation error if JDK 1.4 is used.

SandeepChicago, if you're preparing for SCJP 1.4, none of the options given are correct.
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
F will compile.
F will return true.
 
SandeepChicago Saini
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot for clearing my question.

Tony You have very nice website. I like you bike.
 
Ranch Hand
Posts: 485
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi yoshi

i didn't get the point The answers D and E will return false because the paramater's class type is not the same as the objects where the methods belong.
can any one explain what it mean???
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sandeep,

A,B,C shows the incomparable types error...so they wont compile...
D,E returns false because equals method comparing the references, in Strings only it compares the content..not in others...so it returns false
F also shows compiler error..because equals method needs object as arguement

-Somesh
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can anyone explain why F will or will not compile? Thanks.
 
Parameswaran Thangavel
Ranch Hand
Posts: 485
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi
assuming if equals method didn't overrided then the equals method behave in the same way as ==
the obviously D and E should through the compile time erro.

on the other hand if the equals method is overridden then it will compare the content and also the reference type.since the reference type is not compatible it will return false,rather than compile time error as in case of == method

am i correct in the above conclusion
 
Parameswaran Thangavel
Ranch Hand
Posts: 485
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi
assuming if equals method didn't overrided then the equals method behave in the same way as ==
the obviously D and E should through the compile time erro.

on the other hand if the equals method is overridden then it will compare the content and also the reference type.since the reference type is not compatible it will return false,rather than compile time error as in case of == method

am i correct in the above conclusion
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
F will not compile because many people are afraid to move to Java 1.5.
This apprehension is unfounded and typically based on ill-information.
Once they do move to the latest version of the language, they will find that indeed F does compile and F evaluates to true. I assume this is the language version for which you are studying - the latest.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To be fair, SCJP for 1.5 isn't officially offered yet, unless you caught the beta. SCJP 1.4 will probably still be offered for some time after SCJP 1.5 becomes available. So I would recommend that people in this forum be aware that both 1.4 and 1.5 will be under discussion here. For a while anyway. I'm sure 1.4 will gradually die off, and like Tony I recommend that everyone move to 1.5 as soon as possible. But until that actually happens, we'll all have to be a little careful dealing with questions which have different answers in 1.4 and 1.5.

Thanks to Joyce Lee who was able to clearly and correctly explain what was going on, for both 1.4 and 1.5 - though few seem to have noticed. Perhaps those of you repeatedly asking questions should take a little more time to look at the answers that have gone before.
 
Crusading Chameleon likes the size of this ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic