java.lang.String.toString()


 

java.lang.String.toString()

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.

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

Ads