Dumped in Coldfusion

Friday, November 09, 2007

Convert integers to strings in java

If you have an integer, and want a string, concatenating it with "" is not as good as String.valueOf. It generates loads more code.


int i = 5;

String s1 = "" + i; // BAD
String s2 = String.valueOf(i); // GOOD


I realise that it's a bit more typing, but it is clearer what you want, and it is more efficient.

This also works for all the other unboxed types. long, boolean etc.

0 Comments:

Post a Comment

<< Home