Comparing String Builders – Selected API Classes 05/22/2022 Rivka Scheinberg Post in Implementing an Interface,Oracle Exams Comparing String Builders String builders implement the Comparable<StringBuilder> interface and can be compared lexicographically, analogous to String objects (p. 447). Click here to view code image int compareTo(StringBuilder anotherSB) Here are some examples of string builder comparisons: Click here to view code image StringBuilder sb1 = new StringBuilder(“abba”);StringBuilder sb2 = new StringBuilder(“aha”);int compValue1 = sb1.compareTo(sb2); // negative value => sb1 < sb2int compValue2 = sb2.compareTo(sb1); // positive value => sb2 > sb1boolean isEqual = sb1.compareTo(sb2) == 0; // falseboolean flag = sb1.toString().equals(sb2.toString()); // false Modifying String Builders Appending, inserting, replacing, and deleting characters automatically results in adjustment of the string builder’s structure and capacity, if necessary. The indices passed as arguments in the methods must be equal to or greater than 0. An IndexOutOfBoundsException is thrown if an index is not valid. Note that the methods in this subsection return the reference value of the modified string builder, making it convenient to chain calls to these methods. Appending Characters to a String Builder The overloaded method append() can be used to append characters at the end of a string builder. Click here to view code image StringBuilder append(Object obj) The obj argument is converted to a string as if by the static method call String.valueOf(obj), and this string is appended to the current string builder. Click here to view code image StringBuilder append(String str)StringBuilder append(CharSequence charSeq)StringBuilder append(CharSequence charSeq, int start, int end)StringBuilder append(char[] charArray)StringBuilder append(char[] charArray, int offset, int length)StringBuilder append(char c) Allow characters from various sources to be appended to the end of the current string builder. Click here to view code image StringBuilder append(boolean b)StringBuilder append(int i)StringBuilder append(long l)StringBuilder append(float f)StringBuilder append(double d) Convert the primitive value of the argument to a string by applying the static method String.valueOf() to the argument, before appending the result to the string builder.