Home Tutorial Java StringExamples java.lang.String.toString()

 
 

java.lang.String.toString()
Posted on: April 6, 2005 at 12:00 AM
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

Related Tags for java.lang.String.toString():


Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.