String Conversion and toString() in Java

By: Mashoud Printer Friendly Format    


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
System.out.println(s);
}
}

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( ).



Most Viewed Articles (in Java )

Latest Articles (in Java)

Comment on this tutorial

Subscribe to Tutorials

Archived Comments

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

2. This solve my problem to use toString() method. Th
View Tutorial          By: Simon at 2009-07-31 08:56:36

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

4. Good example.

Leslie V
www.g

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

5. Good and Effective example
thank you

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

6. it is unfair to leave this portal without thanking
View Tutorial          By: anil kumar at 2009-12-30 23:40:25

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

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

9. Yes, finaly toString start making sense to me, tha
View Tutorial          By: Ted at 2010-04-10 19:15:50

10. I didnt get the use of public Sting toString().. I
View Tutorial          By: Pradesh at 2010-05-06 05:38:23

11. sir... i made a program just like that ... but the
View Tutorial          By: David at 2010-07-08 03:34:13

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

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

14. Hi thanks for the example for overriding toString(
View Tutorial          By: Reshma at 2011-05-11 10:46:08

15. this article is excellent....solve all my doubt re
View Tutorial          By: pramodk at 2011-07-18 14:22:18

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

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

18. Thanks a lot. In a very Simple way you explained.<
View Tutorial          By: Shoaeb uddin at 2012-01-13 18:53:45

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

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

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

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

23. Your explanation and example for toString() is gre
View Tutorial          By: Dinesh at 2012-10-19 08:29:13

24. I will not contest. Certainly this is extremely im
View Tutorial          By: Bangabee at 2012-11-10 18:17:40

25. How much time you take from copying this dicto lin
View Tutorial          By: Rahul Gangwar at 2015-01-28 07:34:23

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