I am using the following approach to convert any primitive data type to string
Example
int i = 5;// String convertToString = ""+i;// convert any primitive data type to string
To convert int data type to string i can use Integer.toString()
but what is the better way to convert any type of primitive data (not only int) types to string
toString() The Integer. toString() method converts int to String. The toString() is the static method of Integer class.
Converting a primitive value (an int, for example) into an object of the corresponding wrapper class (Integer) is called autoboxing. The Java compiler applies autoboxing when a primitive value is: Passed as a parameter to a method that expects an object of the corresponding wrapper class.
Casting between primitive Java types. Changing a value from one data type to a variable of another type is known as data type conversion. There are two types of casting, Primitive Type Casting. Reference Type Casting.
Use String.valueOf()
method.
int no = 2; String strValue = String.valueOf( no );
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With