Skip to content

Convert Primitive values to String Example

This example shows how to convert Java primitive data type to String. This example shows how to convert int to String, long to String, float to String, double to String, boolean to String, char to String, short to String and byte to String.

How to convert primitive to String in Java?

There are several ways in which Java primitive data types can be converted to String as given below.

1) Using the valueOf() method of the String class

The String class has a valueOf method which converts primitive types to a string. The valeof method is overloaded for each of the primitive types so that the same method can be used for all the primitive types.

a) Convert int to String

Use the valueOf(int i) of String class to convert from an int to string.

b) Convert float to String

Use the valueOf(float f) method to convert from a float to string

c) Convert double to String

Use the valueOf(double d) method to convert from double to a string

d) Convert boolean to String

Use the valueOf(boolean b) method to convert from boolean to a string

e) Convert char to String

Use the valueOf(char c) method to convert from char to a string

f) Convert long to String

Use the valueOf(long l) method to convert from long to a string

2) Using the toString method of the Wrapper classes

Each of the Java Wrapper class has a toString static method which can be used to convert respective primitive data type to a string object.

a) Convert int to String

Use the toString method of the Integer class to convert from an int to a string.

b) Convert float to String

Use the toString method of the Float wrapper class to convert from a float to a string.

c) Convert double to String

Use the toString method of the Double class to convert from a double to a string.

d) Convert boolean to String

Use the toString method of the Boolean class to convert from boolean to a string.

e) Convert char to String

Use the toString method of the Character class to convert from char to a string.

f) Convert long to String

Use the toString method of the Long class to convert from long to a string.

3) Using the String concatenation (Not recommended)

String concatenation can be used to convert Java primitive values to a string representation as given below.

What is the best way to convert from a primitive value to a String?

The valueOf method of the String class internally calls the toString method of the respective wrapper class to convert the value to a string. So, using either of them is a recommended approach. Prefer toString method of the respective wrapper class whenever possible.

String concatenation should be avoided for two reasons

1) It does not visually tell the intention of the statement, i.e. string concatenation is not meant for the conversion purpose
2) It creates several temporary objects to complete the conversion which is totally unnecessary. When the compiler encounters a statement like

it is actually converted into something like the code given below (StringBuffer or StringBuilder depending on the Java version)

This example is a part of the Java Basic Examples and String in Java tutorial.

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

About the author

Leave a Reply

Your email address will not be published.