I’ve heard that the compiler (or was it the JVM?) will automatically use a StringBuilder for some string concatenation. When is the right time to explicitly declare one? I don’t need a StringBuffer for being thread-safe.
Thanks.
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.
The compiler will use it automatically for any string concatenation using “+”.
You’d usually use it explicitly if you wanted to concatenate in a loop. For example:
Another situation would be where you wanted to build up a string over multiple methods, or possibly conditionalise some bits of the building. Basically, if you’re not building the string in a single statement (where the compiler can help you) you should at least consider using
StringBuilder.