< Prev
Next >



toString() method









Signature of toString() method -


 public String toString() 





Note :


toString() method is implicitly called when any object of a class is printed.




toString() example


toString() method is implicitly called when an object is printed.
class A
{ 

public static void main(String... ar)
{
A ob = new A();
System.out.println(ob); //object ob is printed, hence toString() method is implicitly called here
}
}


Output


A@70dea4e


Program Analysis


By directly printing the object of A class, we have got its String representation that is described as -




Overridding toString() method to give a same message for every object.


We have overridden toString() method in a class in order to give a same message for of its objects, when any of them are directly printed.
class A
{ 
int a;
public String toString()
{
return "We have created an object";
}


public static void main(String... ar)
{
A ob1 = new A();
ob.i=10;
System.out.println(ob1);

A ob2 = new A();
ob2.i=20;
System.out.println(ob2);
}
}


Output -


We have created an object
We have created an object


Program Analysis


We have created two objects of A class and toString() method gives a same basic repetitive message for each of these object, when it is directly printed.




Overridding toString() method to give a different message for every object.


Now we are going override toString() method in a class, to give a specific message for each object, when it is directly printed.
class Database
{ 
String name;
String city;
int age;
float donation;


public void putName(String str)
{
name = str;
}

public void putCity(String str)
{
city = str;
}

public void putAge(int i)
{
age = i;
}

public void putDonation(Float f)
{
donation = f;
}

public String toString()	//Overriding toString() method 
{
return name + " from " + city + " aged " + age + " has donated - " + donation + "$";
}


public static void main(String... ar)
{
Database ob1 = new Database();
ob1.putName("Howard");
ob1.putCity("New York");
ob1.putAge(40);
ob1.putDonation(1000.50f);
System.out.println(ob1); 	//Printing the first object directly

Database ob2 = new Database();
ob2.putName("Steve");
ob2.putCity("Sydney");
ob2.putAge(30);
ob2.putDonation(500.50f);
System.out.println(ob2); 	//Printing the second object directly

}
}


Output-


Howard from New York aged 40 has donated - 1000.5$
Steve from Sydney aged 30 has donated - 500.5$


Program Analysis


In the last code, we have a class Database in which we have overridden toString() method to give a specific message that presents values in a particular printed object.





Number classes have already overridden toString() method of Object class.


Number classes like Integer, Float, Double, Byte, Short have already overridden toString() method of Object class, hence, when their object is directly printed, a readable form of an object is displayed.
class A
{ 
public static void main(String... ar)
{
Integer i = new Integer(40);
Float f = new Float(10.4);
Double d = new Double(2222.2222);

System.out.println(i);
System.out.println(f);
System.out.println(d);
}
}


Output is


40
10.4
2222.2222




Please share this article -

Facebook Google Pinterest Reddit Tumblr Twitter




< Prev
Next >
< ignoreCase() method
StringBuffer class >

Please Subscribe

Please subscribe to our social media channels for daily updates.


Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page

Notifications



COMING SOON
-
PYTHON