| Author |
toString method
|
abalfazl hossein
Ranch Hand
Joined: Sep 06, 2007
Posts: 602
|
|
Hello
Returns a string representation of the object.
http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Object.html#toString()
Can't we write our own method to return a string?If it is all about return a string....
May someone explain more about this:
a string representation of the object
|
 |
Jimi Svedenholm
Ranch Hand
Joined: May 19, 2001
Posts: 53
|
|
abalfazl hossein wrote:
Can't we write our own method to return a string?If it is all about return a string....
Yes, you can most definitely write your own toString method for your own objects. If you haven't tried to do that yet then I suggest that you do. If you have any problems then you can post your code here and maybe someone can point out the error in your code.
May someone explain more about this:
a string representation of the object
Well, that is really only the short description. If you click the link "toString()" you will see the longer description. In short, this method should return a String that represents this object. For example, if the object is an instance of a class named Person, then to toString() method could return a String containing the persons name, phone number etc. Like: "John Doe, 1-23-456-789".
|
 |
Matthew Brown
Bartender
Joined: Apr 06, 2010
Posts: 3793
|
|
abalfazl hossein wrote:
Can't we write our own method to return a string?If it is all about return a string....
You can write as many methods as you want that return strings. toString() is special, though, because it's called automatically in certain circumstances where the Object is converted to a String.
For example, if anObject is any object you like:
is equivalent to
Because of this, it's a good idea to provide your own implementation of toString() whenever there's a sensible way to do it that's more appropriate than the built-in version.
|
 |
 |
|
|
subject: toString method
|
|
|