This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
Returns a string representation of the object. In general, the toString method returns a string that "textually represents" this object. The result should be a concise but informative representation that is easy for a person to read. It is recommended that all subclasses override this method.
The toString method for class Object returns a string consisting of the name of the class of which the object is an instance, the at-sign character `@', and the unsigned hexadecimal representation of the hash code of the object. In other words, this method returns a string equal to the value of:
public means that the method is visible and can be called from other objects of other types. String tells you that the method's return type (output) is a String. The empty parenthesis in toString() tell you that you don't have to pass in anything to the method.
Abid Ramay wrote:
Returns a string representation of the object. In general, the toString method returns a string that "textually represents" this object.
This is the summary of the method's function. This also tells you that the toString() can be used on any object (since it is defined in the Object class).
The best way to find out exactly how a method works, however, is to try it out.
Try something like:
And try different things. For example, using the toString method on objects like Integer or Boolean simply returns their values as a String.