The StringBuilder Class – Selected API Classes 09/22/2021 Rivka Scheinberg Post in Determining the Range,Oracle Exams,Static Member Types 8.5 The StringBuilder Class Although there is a close relationship between objects of the String and StringBuilder classes, these are two independent final classes, both directly extending the Object class. Hence, String references cannot be stored (or cast) to StringBuilder references, and vice versa. However, both classes implement the CharSequence interface (p. 444) and the Comparable interface (§14.4, p. 761). Since the StringBuilder class does not override the equals() and hashCode() methods from the Object class, the contents of string builders should be converted to String objects for equality comparison and to compute a hash value. The StringBuilder class has many operations analogous to the ones in the String class. In addition, it provides support for manipulating mutable strings (p. 467). Constructing String Builders Mutability In contrast to the String class, which implements immutable character sequences, the StringBuilder class implements mutable character sequences. Not only can the character sequences in a string builder be changed, but the capacity of the string builder can also change dynamically. The capacity of a string builder is the maximum number of characters that a string builder can accommodate before its size is automatically augmented. The legacy class StringBuffer (Figure 8.1, p. 425) also implements mutable sequences of characters. It support the same operations as the StringBuilder class, but the StringBuffer class is thread-safe. Certain operations on a string buffer are synchronized so that when accessed concurrently by multiple threads, these operations are safe to perform without corrupting the state of the string buffer (§22.4, p. 1387). Note that a String object is also thread-safe—because it is immutable, a thread cannot change its state. String builders are preferred when heavy modification of character sequences is involved and synchronization of operations, which also carries a performance penalty, is not important. Although the rest of this section focuses on string builders, it is equally applicable to string buffers. String Builder Constructors The final class StringBuilder provides four constructors that create and initialize StringBuilder objects and set their initial capacity. Click here to view code image StringBuilder(String str)StringBuilder(CharSequence charSeq) The contents of the new StringBuilder object are the same as the contents of the String object or the character sequence passed as an argument. The initial capacity of the string builder is set to the length of the argument sequence, plus room for 16 more characters. Click here to view code image StringBuilder(int initialCapacity) The new StringBuilder object has no content. The initial capacity of the string builder is set to the value of the argument, which cannot be less than 0. StringBuilder() This constructor also creates a new StringBuilder object with no content. The initial capacity of the string builder is set to 16 characters. Examples of StringBuilder object creation and initialization: Click here to view code image StringBuilder strBuilder1 = new StringBuilder(“Phew!”); // “Phew!”, capacity 21StringBuilder strBuilder2 = new StringBuilder(10); // “”, capacity 10StringBuilder strBuilder3 = new StringBuilder(); // “”, capacity 16