The StringBuilder
class in Java is a mutable sequence of characters. It is part of the java.lang
package and extends the AbstractStringBuilder
class, which in turn extends the Object
class.
Here's the class hierarchy for the StringBuilder
class:
Object AbstractStringBuilder StringBuilder
The StringBuilder
class provides methods for constructing and modifying a string in a more efficient way than using the String
class, which is immutable. It is often used when you need to perform multiple operations on a string and want to avoid creating multiple intermediate String
objects.
Here's an example of how you can use the StringBuilder
class to build a string:
StringBuilder sb = new StringBuilder(); sb.append("Hello"); sb.append(", "); sb.append("world"); String s = sb.toString(); // s is "Hello, world"
You can find more information about the StringBuilder
class and examples of how to use it in the Java documentation.