• 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

toString method confusion.

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


when i try to compile and run above code it prints "Confusion" as output.
As we all know if you try to print any class object in java it should print "objectname@some hascode" .

I found above code in one mock test which says it would call toString method .....but why why why???

i am really confused with this behaviour of compiler....can any one please explain in details ....what is going on here.....

Thanks in advance.
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

vikram Kesav wrote:
As we all know if you try to print any class object in java it should print "objectname@some hascode" .



No! It's not that. When you print any class object, then the toString() method of that class is called. If the class doesn't override the toString method then the toString method of it's super class is executed. If the super class of a class doesn't override the toString method or if the super class of a class is Object, then the toString method of Object class is executed which displays objectname@some hascode. So basically objectname@some hascode is displayed only if the toString method of Object class is executed...
 
reply
    Bookmark Topic Watch Topic
  • New Topic