• 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

equals()

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Question:
Which of these statements are true?
A. If the references x and y denote two different objects, then the experssion x.equals(y) is always false.
B. All array objects have a method named clone.
Answer is B only. Why isn't answer A. true???
Thanks
 
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
x.equals(y); could be false because the equals() method tests the value of the object not the reference.
however, i don't think it would always be false, because if both objects have the same values then it would be true.
 
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For instance:
String String1 = "abc";
String String2 = "abc";
String1.equals(String2) returns true.
Marilyn
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Marilyn,
While your answer is technically correct, I feel it is not the right example in this case because String1==String2will return true also.
Ambrose
 
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Bob,
The equals method is defined in the Object class and it does nothing but basic reference comparison. Several subclasses of Object override the equals method to do content based comparison. The examples are the wrapper classes and the String class.
However, there are some derivatives of Object that doesnot override the equals method. One such class is the StringBuffer. If a class (derived from Object ) does not override the equalsmethod, then as per the OO rules, calling equals method on an instance of such a class will call the Object.equals() method.... and you know what happens
Hence the answer A is false!
Hope that helps,
Ajith
[This message has been edited by Ajith Kallambella (edited October 10, 2000).]
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
If i understand correctly.
If the equals() method is not overridden then the objects
equals() method is called which is same as ==
Thanks

------------------
Subbu
 
Marilyn de Queiroz
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, Subbu, that is true.
Marilyn
 
A day job? In an office? My worst nightmare! Comfort me tiny ad!
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic