The
toString
Method
A toString() is an in-built method in Java that returns the value given to it in
string format. Hence, any object that this method is applied on, will then be
returned as a string object.
All classes have a toString method. Its purpose is to return a String
representing the data of the object.
The toString method is called automatically (implicitly) when an object
reference is used with the print or println
methods.
How to Use toString method
class Student
{
int id;
String name;
String dept;
public String toString()
{
return id+" "+name+" "+dept;
}
}
|