What is better
int i = 45;
String str = "dsfgdsgf"+i;
or
int i = 45;
String str = new StringBuilder().append("dsfgdsgf").append(i).toString();
I have read somewhere that StringBuilder is always better than concatenating strings
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
There is no difference in performance, as the compiler will internally convert the first version to the second one.
Since the first version is more readable you should use it when you concatenate a fixed number of items into a string.
Use
StringBuilderwhen you append to a string many times, for example in a loop.