I want to append an integer to a string. Is there a difference between doing:
String str = "Number: " + myInt;
and
String str = "Number: " + Integer.toString(myInt);
In other words, should I bother using the Integer.toString()
method, when not necessary?
Edit: I am not wondering "How to convert an integer to string", but "if I am required to use a certain method, to convert".
There's no difference. The compiler (Oracle JVM 1.8) transform both snippets to
(new StringBuilder()).append("Number: ").append(myInt).toString();
Personally, I wouldn't use Integer.toString()
as it adds noise to the code, and that doesn't provide clarity nor readability.
I made a mistake on the original answer and described that there would be a minor difference (see the answer history if you want!)
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