This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

about "toString"

 
lang lang
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am really confused about it .
would you tell me about it and how to use it?
would you take some examples for me ?
thank you!
 
David Crossett
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The 'toString' method is inherited by all classes, and is originally defined in the Object class (the super parent of all objects/classes). The toString() method needs to be over-ridden (this means to replace the super-classes method definition with your own - just like when you write an applet, you over-ride the init(), start(), and paint() methods with methods to do something else you want). The original definition of toString() doesn't do too much (I'm not sure what it does at all). When you over-ride it in a program, you define how you would like YOUR data converted to 'String' data. For example, you may want to convert date/time data into a format you prefer.


One thing to know also is this: whenever you concatenate a String with any other object, an implicit call is made to the 'toString()' method. Keep this in mind if you over-ride 'toString()' and then can't figure out why you're strings look so different than you might be used to!

David
- still nothing to say
 
David Crossett
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let me add that you'll get more help if you conform to the naming rules, i.e. use your name or at least one that sounds real.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic