• 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

Doubt in equals method

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, I executed the following code and got the output as : success
I could not understand the output.since we call the equals method of the object class, which is not overriden.It should return Failed.But why does it gives success as output.could anyone explain me clearly about the equals.

public static void main(String[] args) {
String s = new String("hi");
Object o = new String("hi");
if(o.equals(s)) {
System.out.println("success");
} else {
System.out.println("Failed");
}
}

If the lines has been
String s1 = new String("hi");
String s2 = new String("hi");
then it means only one string obj is created and both s1 ans s2 refer to it.so equals shud retruns true since it has overridden.

But in the above example, we call the equals on object class which does not override equals..so actually two objects are created.but how come it retirns that they are equal?

Thanks,
Sumi
 
Ranch Hand
Posts: 558
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sumi,

You did n't get OO concept Polymorphism.

Object o = new String("hi");

when we call o.eqauls() equals of String is is executed.


Take this example:

 
sumi selva
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Thank you very much.Now got cleared.

Thanks,
Sumi
 
The harder I work, the luckier I get. -Sam Goldwyn So tiny. - this ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic