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
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: 9453

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
It's not a secret anymore!