In Java, String is immutable, meaning we cannot change it once after created. Makes it less efficient for frequently modified text. Conversely, StringBuffer is mutable and thread-safe, ideal for strings undergoing frequent changes, especially in multi-threaded contexts.
In Java, a string is a collection of characters. It is found in the java.lang package, it is one of the most popular classes in Java. One of the unique characteristics of a string is that, once generated, it cannot be altered. An entirely new String object is created for every change.
To read more StringBuffer in Java
Strings can be declared in two ways:
1. Using string literals (stored in String Constant Pool)
2. Using the new keyword (stored in heap memory)

The code demonstrates that Java String objects are immutable; the concat(" World") method creates a new string, but since it is not assigned back to s, the original string remains unchanged and outputs "Hello".
Output:
Hello
This code shows how string storage works in Java: s1 and s2 point to the same object in the String Constant Pool, so == returns true, while s3 creates a new heap object, making s1 == s3 false; however, equals() checks content, so it returns true.
Output:
true false true
This code demonstrates various String methods in Java: toUpperCase() and toLowerCase() change case, substring() extracts part of the string, charAt() retrieves a character at a given index, length() gives the total characters, indexOf() finds the position of a character, equals() compares content, and replace() substitutes characters.
Output:
PROGRAMMING Progra g 11 programming 6 true PrograXXing
A Java class called StringBuffer is used to construct mutable strings. In contrast to String, a StringBuffer object is modifiable after it is created. Because it is synchronized, multi-threaded programs can use it without bothering about thread safety.
To read more StringBuffer in Java

This code shows that StringBuffer is mutable in Java; the append(" World") method directly modifies the existing object's content, so printing sb outputs "Hello World".
Output:
Hello World
This code demonstrates StringBuffer mutability using insert() to add "Java " at index 6, temporarily modifying the string, and delete() to remove characters from index 6 to 11, restoring it back to "Programming".
Output:
PrograJava mming Programming
This code illustrates StringBuffer operations where reverse() inverts the entire sequence of characters, and replace(1, 3, "XX") substitutes the characters from index 1 to 2 with "XX", updating the content accordingly.
Output:
EDCBA EXXBA
| Characteristics | String | StringBuffer |
|---|---|---|
| Definition | Represents a sequence of characters that is immutable. | Represents a sequence of characters that is mutable. |
| Mutability | Immutable → once created, cannot be changed. | Mutable → contents can be modified without creating new objects. |
| Declaration Syntax | String s = "Hello"; | StringBuffer sb = new StringBuffer("Hello"); |
| Modification Behavior | Every modification (like concatenation) creates a new object. | Modifications happen in the same object (e.g., append, insert, delete). |
| Thread Safety | Thread-safe by nature (immutable objects are safe to share). | Methods are synchronized → thread-safe but slightly slower. |
| Performance | Slower for frequent modifications due to object creation overhead. | Faster for modifications since the object is mutable. |
| Memory Usage | Stored in String Constant Pool (SCP) if created with literals. | Stored in heap memory, capacity grows dynamically. |
| Useful Methods | length(), substring(), charAt(), toUpperCase(), replace(). | append(), insert(), delete(), reverse(), replace(). |
| Usage Scenarios | Best when data is constant or rarely changed (e.g., config values). | Best when data is frequently modified (e.g., logs, dynamic strings). |
| Example | String s = "Hello"; s.concat(" World"); → s remains "Hello". | StringBuffer sb = new StringBuffer("Hello"); sb.append(" World"); → modified. |
The main difference between String and StringBuffer is essential for effective Java character sequence handling. String objects are immutable, making them naturally thread-safe and ideal for constant text. In contrast, frequent modifications with Strings cause performance overhead, as each operation creates a new object.
1. Which of the following correctly describes the mutability of String and StringBuffer in Java?
Answer: B
Explanation: In Java, once a String object is created, it cannot be changed (immutable). In contrast, StringBuffer objects allow modifications like append, insert, or delete directly on the existing object.
2. Consider the following code and find the output.
Answer: A
Explanation: While the concat() method creates a new object, it is not allocated back to s because String is immutable. Consequently, "Hello" is still denoted by s.
3. Which of the following is true about StringBuffer?
Answer: B
Explanation: StringBuffer methods are safe for multi-threaded configurations since they are synchronized. Changes are made directly to the object because it is changeable.
4. Which operation in StringBuffer allows directly modifying the content of the same object instead of creating a new one?
Answer: C
Explanation: StringBuffer's append() method reflects mutability by appending new material to the existing object. No new object is created, in contrast to String.concat().
5. Consider the following code and find the output.
Answer: A
Explanation: s1 and s2 both point to the same object in the String Constant Pool, so == returns true. s3 is created in heap memory, making s1 == s3 false, but equals() checks content, returning true.
We request you to subscribe our newsletter for upcoming updates.

We deliver comprehensive tutorials, interview question-answers, MCQs, study materials on leading programming languages and web technologies like Data Science, MEAN/MERN full stack development, Python, Java, C++, C, HTML, React, Angular, PHP and much more to support your learning and career growth.
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India