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

toString method

abalfazl hossein
Ranch Hand

Joined: Sep 06, 2007
Posts: 606


If all this mehtod do is return a string, Then I can define any method can do this! What is the real mission of this method?
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

Because every Object has the toString() method you can iterate through a Collection of objects and print the content. Also it is often used for debugging and logging purposes.


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 13875
    
  10

abalfazl hossein wrote:If all this mehtod do is return a string, Then I can define any method can do this! What is the real mission of this method?

I don't understand your reasoning. It's as if you think that the purpose of the toString() method is so simple that it's not worthy of existence. As if every method you write has to do something important and complicated, something that you really have to think hard about, otherwise it's not worth it

Ofcourse you can write any other method that returns a string representation of the object. But it will not work in the same way as the toString() method, which will be called automatically when you do something like this:


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: toString method
 
Similar Threads
Please solve my problem
(Inheritance) please Help!
can we invoke an overloaded constructor by object of same class?
Inheritance, and the mis-use of the 'static' keyword
Using toString().......