Java Basic Examples

Convert Primitive to String Example

This Java example shows how to convert any primitive data type to String object.

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) Convert primitive to String Using String.valueOf() static method

Java String class has valueOf() method which converts primitive type to String. String.valeof() method is overloaded for each of the primitive type so that the same method can be used regardless of the type you want to convert to String.

a) Convert int to String

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

b) Convert float to String

Use valueOf(float f) method to convert from float to String

c) Convert double to String

Use valueOf(double d) method to convert from double to String

d) Convert boolean to String

Use valueOf(boolean b) method to convert from boolean to String

e) Convert char to String

Use valueOf(char c) method to convert from char to String

f) Convert long to String

Use valueOf(long l) method to convert from long to String

 

2) Convert primitive to String Using Wrapper classes

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

a) Convert int to String

Use Integer.toString(int i) method to convert from int to String.

b) Convert float to String using Integer wrapper class

Use Float.toString(float f) method to convert from float to String.

c) Convert double to String using Double wrapper class

Use Double.toString(double d) method to convert from double to String.

d) Convert boolean to String using Boolean wrapper class

Use Boolean.toString(boolean b) method to convert from boolean to String.

e) Convert char to String using Character wrapper class

Use Character.toString(char c) method to convert from char to String.

f) Convert long to String using Long wrapper class

Use Long.toString(long l) method to convert from long to String.

 

3) Using String concatenation (Not recommended)

String concatenation can be used to convert Java primitive values to String representation like given below.

What is the best way to convert from Java primitive to String?

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

String concatenation should be avoided where possible for two reasons

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

it is actually converted into something like this,

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