• 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

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


when im not yet Overriding the toString() method the output is
this:


but when i include the toString() method
the output became fine:


but i only added the toString() method... i didnt use this method in any of my predefined methods? how does it affects the correct output?

by the way, to be honest i've already posted this thread in other forums, but i need some more clarification of why this happened...
http://www.java-forums.org/new-java/24496-returning-object-method.html - cross posted

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if you don't override toString then the toString implementation of Object is used, since that's the only class your class extends. This returns a string that identifies the class of your object and somthing like an address.
 
jhon max
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

if you don't override toString then the toString implementation of Object is used, since that's the only class your class extends



so if i dont override the toString method properly, then my class will AUTOMATICALY extend as an object? and my Class
will use the Object class's toString() method to make a string representation of its object...?

---

in my case im reffering to the String class? not in the object class?


is that how the toString() of String class works and differs from the toString() method of Object class?
 
Jack Ploeg
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
EVERY class extends the Java base-class Object. Check here: Java Object So every class automatically inherits this Object class's implementation of toString. As you found out you can override this to do whatever you want it to, but if you don't then the default implementation is used.
 
Ranch Hand
Posts: 710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

jhon max wrote:

if you don't override toString then the toString implementation of Object is used, since that's the only class your class extends



so if i dont override the toString method properly, then my class will AUTOMATICALY extend as an object? and my Class
will use the Object class's toString() method to make a string representation of its object...?

---

in my case im reffering to the String class? not in the object class?


is that how the toString() of String class works and differs from the toString() method of Object class?



Every class extends Object, no matter what. So if you call toString() on any class that doesn't override the method, it will walk up the hierarchy to find the most specific toString(). If there isn't one, it will use the Object class's toString() method.
 
jhon max
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hmm this is a bit confusing and for me ryt now... anyway tnx for the answers!!!
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

jhon max wrote:hmm this is a bit confusing and for me ryt now... anyway tnx for the answers!!!


Please use real words when posting to the forums. Abbreviations such as "ryt" in place of "right" and "tnx" for "thanks" only serve to make your posts more difficult to read and less likely to generate useful responses.

Please read this for more information.

thanks,
bear
JavaRanch sheriff
 
jhon max
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
oh sorry sir... wont happen again.. anywany thanks for letting me know about that,,

 
Paper jam tastes about as you would expect. Try some on this 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