String Conversion and toString() in Java

By: Mashoud  

When Java converts data into its string representation during concatenation, it does so by calling one of the overloaded versions of the string conversion method valueOf( ) defined by String. valueOf( ) is overloaded for all the simple types and for type Object. For the simple types, valueOf( ) returns a string that contains the human-readable equivalent of the value with which it is called. For objects, valueOf( ) calls the toString( ) method on the object. We will look more closely at valueOf( ) later in this chapter. Here, let's examine the toString( ) method, because it is the means by which you can determine the string representation for objects of classes that you create.

Every class implements toString( ) because it is defined by Object. However, the default implementation of toString( ) is seldom sufficient. For most important classes that you create, you will want to override toString( ) and provide your own string representations. Fortunately, this is easy to do. The toString( ) method has this general form:

String toString( )

To implement toString( ), simply return a String object that contains the human-readable string that appropriately describes an object of your class.

By overriding toString( ) for classes that you create, you allow the resulting strings to be fully integrated into Java's programming environment. For example, they can be used in print( ) and println( ) statements and in concatenation expressions. The following program demonstrates this by overriding toString( ) for the Box class:

// Override toString() for Box class.
class Box {
double width;
double height;
double depth;
Box(double w, double h, double d) {
width = w;
height = h;
depth = d;
public String toString() {
return "Dimensions are " + width + " by " +
depth + " by " + height + ".";
class toStringDemo {
public static void main(String args[]) {
Box b = new Box(10, 12, 14);
String s = "Box b: " + b; // concatenate Box object
System.out.println(b); // convert Box to string

The output of this program is shown here:

Dimensions are 10 by 14 by 12.
Box b: Dimensions are 10 by 14 by 12.

As you can see, Box's toString( ) method is automatically invoked when a Box object is used in a concatenation expression or in a call to println( ).

Archived Comments

1. thanks you thear
View Tutorial          By: phim heo at 2015-12-22 16:01:54

2. How much time you take from copying this dicto line by line even, your Example from Complete referen
View Tutorial          By: Rahul Gangwar at 2015-01-28 07:34:23

3. I will not contest. Certainly this is extremely important information about forString() will save my
View Tutorial          By: Bangabee at 2012-11-10 18:17:40

4. Your explanation and example for toString() is great. It cleared my doubt. Thanks..
View Tutorial          By: Dinesh at 2012-10-19 08:29:13

5. It is very useful for me guyzz......thanx.......:-)
View Tutorial          By: Rajesh at 2012-09-29 10:11:45

6. this helped a lot!!!!
View Tutorial          By: karthik rk at 2012-09-04 16:32:25

7. How toString() should be explained!
View Tutorial          By: Mike at 2012-06-08 19:36:50

8. Good stuff and good example to support it.
View Tutorial          By: tabrez at 2012-04-10 01:44:48

9. Thanks a lot. In a very Simple way you explained.

Shoaeb Cognizant..

View Tutorial          By: Shoaeb uddin at 2012-01-13 18:53:45

10. thanks. good job
View Tutorial          By: Lahiru chathuranga at 2011-08-30 03:56:26

11. Thanks ,got useful information on toString().
View Tutorial          By: jaffar at 2011-08-01 10:15:04

12. this article is excellent....solve all my doubt regarding tostring().thnx
View Tutorial          By: pramodk at 2011-07-18 14:22:18

13. Hi thanks for the example for overriding toString() function. Solved my problem.
View Tutorial          By: Reshma at 2011-05-11 10:46:08

14. Thanks! It helps me a lot
View Tutorial          By: ajikoh at 2011-03-24 02:01:02

15. good job.thanks
View Tutorial          By: bistrita at 2010-09-09 09:33:31

16. sir... i made a program just like that ... but there is an error "C:\Documents and Settings\ali
View Tutorial          By: David at 2010-07-08 03:34:13

17. I didnt get the use of public Sting toString().. I am a bigenner in java .. please send a solution w
View Tutorial          By: Pradesh at 2010-05-06 05:38:23

18. Yes, finaly toString start making sense to me, thanks a lot
View Tutorial          By: Ted at 2010-04-10 19:15:50

19. This example really works and help a lot me to disloge my confusion.
View Tutorial          By: Monu Kumar at 2010-03-30 09:26:23

20. that was useful to me, thank you so much .
View Tutorial          By: ahmed at 2010-01-11 15:59:42

21. it is unfair to leave this portal without thanking you.this example explained me the role of toStrin
View Tutorial          By: anil kumar at 2009-12-30 23:40:25

22. Good and Effective example
thank you

View Tutorial          By: Perumal Naren at 2009-12-30 03:10:50

23. Good example.

Leslie V

View Tutorial          By: Leslie V at 2009-10-14 00:47:06

24. This was very helpful! Thanks!!!
View Tutorial          By: Andreas Clausen at 2009-09-03 15:07:14

25. This solve my problem to use toString() method. Thank you very much it is great!!!
View Tutorial          By: Simon at 2009-07-31 08:56:36

26. hai this is being copied from Complete reference
View Tutorial          By: kar at 2008-03-31 00:17:52

Most Viewed Articles (in Java )

Latest Articles (in Java)

Comment on this tutorial