Java convert double to String example

This example shows how to convert double to String in Java using toString, valueOf, and + operator. It also shows the best way to convert among them.

How to convert double to String in Java?

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

1) Convert double to String using the String class

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

The valueOf method returns a string representation of double argument.

Output

2) Using the Double wrapper class

Use the toString method of the Double wrapper class to convert.

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

Output

3) Using string concatenation

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

Output

What is the best way to convert?

ThevalueOf method of the String class internally calls the toString method of the Double wrapper class to convert from double to a string value. Using either of the methods is equally efficient in terms of the performance. Preferred way is to use the toString method of the 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 the append method of the StringBuilder or StringBuffer class. So the code,

Will run like,

This example is a part of the Java Basic Examples and Java Type conversion Tutorial.

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

About the author

RahimV

RahimV

My name is RahimV and I have over 16 years of experience in designing and developing Java applications. Over the years I have worked with many fortune 500 companies as an eCommerce Architect. My goal is to provide high quality but simple to understand Java tutorials and examples for free. If you like my website, follow me on Facebook and Twitter.

Add Comment

Your email address will not be published. Required fields are marked *