• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • Devaka Cooray
Saloon Keepers:
  • Ganesh Patekar
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • salvin francis
Bartenders:
  • Ron McLeod
  • Frits Walraven
  • Pete Letkeman

toString() method  RSS feed

 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
  • 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
  • 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
  • Quote
  • Report post to moderator
hmm this is a bit confusing and for me ryt now... anyway tnx for the answers!!!
 
Author and ninkuma
Marshal
Posts: 66823
168
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
oh sorry sir... wont happen again.. anywany thanks for letting me know about that,,

 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!