This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Java Interview Guide and have Anthony DePalma on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes ToString method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "ToString method" Watch "ToString method" New topic

ToString method

sumaraghavi ragha
Ranch Hand

Joined: Nov 17, 2006
Posts: 118
HI All

Can any one please tell me what is the use of a toString menthod

With an example

Ankit Garg

Joined: Aug 03, 2008
Posts: 9466

When you try to access the value of an Object (reference variable) like this

String s = "Hello"+obj;

Then the compiler calls the toString() method on that object. So after compilation you code would look like-
String s = "hello"+obj.toString();

As a developer your job is to provide a toString method with your class and return the relevant details about the object as a String from the toString method.


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
I agree. Here's the link:
subject: ToString method
jQuery in Action, 3rd edition