What is the best way to convert any primitive data type to string

Use String.valueOf() method.

int no = 2;

String strValue = String.valueOf( no );

I recently ran some benchmarks to compare ""+myInt vs Integer.toString(myInt).

And the winner is... Integer.toString() !

Because it does not create temporary strings, uses only a adequately-sized char buffer, and some funky algorithms to convert from a digit to its char counterpart.

Here is my blog entry if you read french (or use the sidebar translation widget if you don't)

Tags:

Java