What is the difference between String and StringBuffer in Java?
Is there a maximum size for String?
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.
Stringis used to manipulate character strings that cannot be changed (read-only and immutable).StringBufferis used to represent characters that can be modified.Performance wise,
StringBufferis faster when performing concatenations. This is because when you concatenate aString, you are creating a new object (internally) every time sinceStringis immutable.You can also use
StringBuilderwhich is similar toStringBufferexcept it is not synchronized. The maximum size for either of these isInteger.MAX_VALUE(231 – 1 = 2,147,483,647) or maximum heap size divided by 2 (see How many characters can a Java String have?).More information here.