Basically there are three way to convert an primitive value to string so that if we have requirement to use that primitive as a string then we can you this concept and way of conversion but now the point is out of these three way which one is the best and take less time and doesn't have any performance issue.
using valueOf() which it self used Integer.toString( int, int ) internally to convert it into string type value.
Using toString() with Wrapper class
Every wrapper class is having overridden toString() method with parameter as any primitive type. So we have one more option to make a primitive data as string type by using toString( int i ) . Here also i have used int type but toString() is available with any primitive type as argument. For ex-
toString() method internally uses character array concept to create an String. So this is the most efficient way of converting into string value.
So it is recommended to use any of the either valueOf() or toString().
Note: It is not recommended to convert with the use of empty string .
The three ways of converting primitive into string are :
1. Using Empty string
2. Using valueOf() with String class
3. Using toString() with Wrapper class.
Using Empty String
With the use of an empty string we are creating an new string in the constant string pool and if that string literal is already present in the pool then it will create the object in the heap which points to the pool literal object and return the heap memory address to its original object. For ex-
public class prog {
public static void main(String[] args) {
String s=""+10;
String x1="java10";
String x2="java"+10;
System.out.println(x1.equals(x2)); // true
System.out.println(x1==x2); // false
System.out.println(x1.hashCode()==x2.hashCode());//true
}
}
In the above example ( x2== x1 ) will give false that means s2 and s1 both are different object and which proves that x1 literal get stored in string pool first and then while storing s2 literal it will first check the pool, if present then it will create an new object in the heap memory if we are using new operator that means internally String is created with the use of ' new ' operator.
Using valueOf() with String class
Another option for making a primitive value to string value is the use of valueOf( int i ) defined static in the String class, which return string value and take integer value from its integer type parameter. Here i have used only int type but valueOf() can be used with any primitive type. For ex-
public class prog {
public static void main(String[] args) {
int x=10;
String s1=String.valueOf(x);
System.out.println(s1);
}
}
using valueOf() which it self used Integer.toString( int, int ) internally to convert it into string type value.
Using toString() with Wrapper class
Every wrapper class is having overridden toString() method with parameter as any primitive type. So we have one more option to make a primitive data as string type by using toString( int i ) . Here also i have used int type but toString() is available with any primitive type as argument. For ex-
public class prog {
public static void main(String[] args) {
int x=10;
String s1=Integer.toString(x);
System.out.println(s1);
}
}
toString() method internally uses character array concept to create an String. So this is the most efficient way of converting into string value.
So it is recommended to use any of the either valueOf() or toString().
Note: It is not recommended to convert with the use of empty string .