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.
1 2 3 4 |
int i = 540; String stringObject = String.valueOf(i); System.out.println("int to String: " + stringObject); |
b) Convert float to String
Use the valueOf(float f)
method to convert from a float to string
1 2 3 4 |
float f = 5.40f; String stringObject = String.valueOf(f); System.out.println("float to String: " + stringObject); |
c) Convert double to String
Use the valueOf(double d)
method to convert from double to a string
1 2 3 4 |
double d = 6.344d; String stringObject = String.valueOf(d); System.out.println("double to String: " + stringObject); |
d) Convert boolean to String
Use the valueOf(boolean b)
method to convert from boolean to a string
1 2 3 4 |
boolean b = false; String stringObject = String.valueOf(b); System.out.println("boolean to String: " + stringObject); |
e) Convert char to String
Use the valueOf(char c)
method to convert from char to a string
1 2 3 4 |
char c = 'a'; String stringObject = String.valueOf(c); System.out.println("char to String: " + stringObject); |
f) Convert long to String
Use the valueOf(long l)
method to convert from long to a string
1 2 3 4 |
long l = 1234234l; String stringObject = String.valueOf(l); System.out.println("long to String: " + stringObject); |
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.
1 2 3 4 |
int i = 476; String stringObject = Integer.toString(i); System.out.println("int to String: " + stringObject); |
b) Convert float to String
Use the toString
method of the Float wrapper class to convert from a float to a string.
1 2 3 4 |
float f = 334.234f; String stringObject = Float.toString(f); System.out.println("float to String: " + stringObject); |
c) Convert double to String
Use the toString
method of the Double class to convert from a double to a string.
1 2 3 4 |
double d = 33.432d; String stringObject = Double.toString(d); System.out.println("double to String: " + stringObject); |
d) Convert boolean to String
Use the toString
method of the Boolean class to convert from boolean to a string.
1 2 3 4 |
boolean b = true; String stringObject = Boolean.toString(b); System.out.println("boolean to String: " + stringObject); |
e) Convert char to String
Use the toString
method of the Character class to convert from char to a string.
1 2 3 4 |
char c = 'z'; String stringObject = Character.toString(c); System.out.println("char to String: " + stringObject); |
f) Convert long to String
Use the toString
method of the Long class to convert from long to a string.
1 2 3 4 |
long l = 2332342327l; String stringObject = Long.toString(l); System.out.println("long to String: " + stringObject); |
3) Using the String concatenation (Not recommended)
String concatenation can be used to convert Java primitive values to a string representation as given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
String strValue = ""; int i = 540; strValue = "" + i; float f = 5.40f; strValue = "" + f; double d = 6.344d; strValue = "" + d; boolean b = false; strValue = "" + b; char c = 'a'; strValue = "" + c; long l = 2332342327l; strValue = "" + l; |
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
1 |
strValue = "" + i; |
it is actually converted into something like the code given below (StringBuffer or StringBuilder depending on the Java version)
1 2 3 4 5 6 7 |
StringBuilder stringBuilderObj = new StringBuilder(); //for "" before + stringBuilderObj.append(""); //for i after + stringBuilderObj.append(i); //result will be converted to string using toString method strValue = stringBuilderObj.toString(); |
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.