Java Basic Examples

Java convert double to String example

This example shows how to convert double to String in Java. This example also shows how to convert double to String using several methods and the best way among them.

How to convert double to String in Java?

There are several ways in which double can be converted to String in Java.

1) Convert double to String using String class

Use valueOf static method of String class to convert double to String.

valueOf method returns String representation of double argument.

Output

2) Convert double to String using Double wrapper class

Use toString method of Double wrapper class to convert from double to String in Java.

This method returns String representation of double value passed as a parameter.

Output

3) Convert double to String using String concatenation

String concatenation can be indirectly used to convert Java primitive values to String as given below.

Output

What is the best way to convert double to String?

valueOf method of String class internally calls the toString method of Double wrapper class to convert from double to String value. Using either of the methods is equally efficient in terms of the performance. Preferred way is to use toString method of Double wrapper class.

String concatenation should be avoided mainly for the conversion purpose because,

a) It is difficult to visually understand that the intent of code statement is to conversion.

b) String concatenation operation creates unnecessary temporary objects during the conversion process. String concatenation is achieved using StringBuffer’s or StringBuilder’s append method. So the code,

Will run like,

Please let us know your views in the comments section below.

Want to learn quickly?
Try one of the many quizzes. I promise you will not be disappointed.

Tags