I have been looking at the behaviour of ""+x versus String.valueOf(x) and while the first is slightly shorter and possibly easier to read, my guess is that it involves 3 String instances (one that is probably pooled) while the second would only create the one String instance.
Not a massive difference, but some of them are right in the core of our logic so I thought it was worth pursuing.
Garrett Smith
Ranch Hand
Joined: Jun 27, 2002
Posts: 401
posted
0
Originally posted by David O'Meara: I have been looking at the behaviour of ""+x versus String.valueOf(x) and while the first is slightly shorter and possibly easier to read, my guess is that it involves 3 String instances (one that is probably pooled) while the second would only create the one String instance.
Not a massive difference, but some of them are right in the core of our logic so I thought it was worth pursuing.
Correct me if I'm wrong, but I think it also involves a StringBuilder (implicitly), using +.
A while ago I wandered about the same problem and did some searching around. The conclusion was not using x + "". Both String.valueOf(x) and Integer.toString(x) where considered equally good, while the first one is more consistent for different types and the second one is slightly more readable. So we settled for String.valueOf(x).