The moose likes Beginning Java and the fly likes about Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "about "toString"" Watch "about "toString"" New topic

about "toString"

lang lang

Joined: Nov 01, 2003
Posts: 8
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

Joined: Feb 05, 2003
Posts: 102
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!

- still nothing to say

David Crossett
-nothing important to say, but learnin' plenty-
David Crossett
Ranch Hand

Joined: Feb 05, 2003
Posts: 102
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.
I agree. Here's the link:
subject: about "toString"
jQuery in Action, 3rd edition