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.
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 valueOf(float f)
method to convert from 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 valueOf(double d)
method to convert from double to 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 valueOf(boolean b)
method to convert from boolean to 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 valueOf(char c)
method to convert from char to 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 valueOf(long l)
method to convert from long to String
1 2 3 4 | long l = 1234234l; String stringObject = String.valueOf(l); System.out.println("long to String: " + stringObject); |
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.
1 2 3 4 | int i = 476; String stringObject = Integer.toString(i); System.out.println("int to String: " + stringObject); |
b) Convert float to String using Integer wrapper class
Use Float.toString(float f)
method to convert from float to 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 using Double wrapper class
Use Double.toString(double d)
method to convert from double to 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 using Boolean wrapper class
Use Boolean.toString(boolean b)
method to convert from boolean to 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 using Character wrapper class
Use Character.toString(char c)
method to convert from char to 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 using Long wrapper class
Use Long.toString(long l)
method to convert from long to String.
1 2 3 4 | long l = 2332342327l; String stringObject = Long.toString(l); System.out.println("long to String: " + stringObject); |
3) Using String concatenation (Not recommended)
String concatenation can be used to convert Java primitive values to String representation like 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 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
1 | strValue = "" + i; |
it is actually converted into something like this,
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(); |
Please let us know your views in the comments section below.
Add Comment