Share on Google+Share on Google+

java.lang.String.toString()

Advertisement
ToString class generally used to represent the generated output in human readable or textual format. It extends from java.lang.Object class.

ToString class generally used to represent the generated output in human readable or textual format. It extends from java.lang.Object class.

Syntax:

objectName.toString()

Generally, toString method converts the object's value to a string therefore an object can be represented textually. You can use toString especially for login or debugging purpose. But the main idea behind it is that toString should be used whenever you are looking to change a datatype to string.

a simple example:

int value = 7;

String str = Integer.toString(value); //now str is equal to 7

System.out.println("My value: " + str);

the output is:

My value: 7

In the above example you can see that toString method is used to convert the primitive values into string type.

Example Codes

Generic toString

Convert Date to String

Convert Character into a String

Advertisements

Advertisement

Posted on: April 6, 2005 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.