Is conversion to String using (“” + ) bad practice?

I would always prefer the String.valueOf version: mostly because it shows what you’re trying to do. The aim isn’t string concatenation – it’s conversion to a string, “the string value of i“.

The first form may also be inefficient – depending on whether the compiler spots what you’re doing. If it doesn’t, it may be creating a new StringBuffer or StringBuilder and appending the value, then converting it to a string.

Funnily enough, I have an article about this very topic – written years and years ago; one of the first Java articles on my web site, IIRC.

Leave a Comment