"Greetings, Amigo!"

"Hello, Rishi!"

"You now know enough about strings to learn something interesting. More specifically, a new class that similar to, but not exactly the same as, the String class."

"Sounds intriguing, though it's not entirely clear to me how ordinary strings fall short and why other string-like classes are needed."

"Let's start with the fact that strings are immutable objects in Java."

"How's that? I forgot... Or I never knew to begin with..."

"Let me remind you that immutable objects are those whose states cannot be changed after they are created."

"Ahhh... Why are strings immutable in Java?"

"This was done to make the String class highly optimized and to allow it to be used everywhere. For example, only immutable types are recommended for use as keys in the HashMap collection.

"However, situations often arise when programmers would find it more convenient for the String class to be mutable. They want a class that doesn't create a new substring every time one of its methods is called."

"But what is it good for?"

"Well, suppose we have a very large string and we need to frequently add something to the end of it. In this case, even a collection of characters (ArrayList<Character>) can be more efficient than constantly recreating and concatenating String objects."

"And that's why we need strings that aren't quite the String class?"

"Exactly. The Java language has added a String-like type that can be changed. It is called StringBuilder".

Creating an object

"To create a StringBuilder object based on an existing string, you need to execute a statement like:

StringBuilder name = new StringBuilder(string);

"To create an empty mutable string, you need to use a statement like this:

StringBuilder name = new StringBuilder();

List of methods

"The StringBuilder class has two dozen helpful methods. Here are the most important ones:

Method Description
StringBuilder append(obj)
Converts the passed object to a string and appends it to the current string
StringBuilder insert(int index, obj)
Converts the passed object to a string and inserts at the middle of the current string
StringBuilder replace(int start, int end, String str)
Replaces the part of the string specified by the start..end interval with the passed string
StringBuilder deleteCharAt(int index)
Removes the character with the specified index from the string
StringBuilder delete(int start, int end)
Removes characters within the specified interval from the string
int indexOf(String str, int index)
Searches for a substring in the current string
int lastIndexOf(String str, int index)
Searches for a substring in the current string, starting from the end
char charAt(int index)
Returns the character in the string at the passed index
String substring(int start, int end)
Returns the substring defined by the specified interval
StringBuilder reverse()
Reverses the current string.
void setCharAt(int index, char)
Changes the character at the specified index to the passed character
int length()
Returns the length of the string in characters

"And now I will briefly describe each of these methods to you.

Appending to a string

"To add something to a mutable string (StringBuilder), use the append() method. Example:

Code Description
StringBuilder builder = new StringBuilder("Hi");

Converting to a standard string

"To convert a StringBuilder object to a String object, you just need to call its toString() method. Example

Code Output
StringBuilder builder = new StringBuilder("Hi");
String result = builder.toString();

How do I delete a character?

"To delete a character in a mutable string, you need to use the deleteCharAt() method. Example:

Code Output
StringBuilder builder = new StringBuilder("Hello");
String result = builder.toString();

How do I replace part of a string with another string?

"For this there is the replace(int begin, int end, String str) method. Example:

Code Output
StringBuilder builder = new StringBuilder("Mellow");
builder.replace(2, 5, "Hello!");
String result = builder.toString();
New Java Syntax, level 9, lesson 7
Let's work with StringBuilder
You need to implement 2 methods using a StringBuilder: addTo(String, String[]) and replace(String, String, int, int). 1. The addTo(String, String[]) method adds to the string received as the first parameter, in order, all the strings in the array of strings received as the second parameter and retur

"Sounds convenient. Thank you, Rishi."

"Don't mention it. How about this, can you flip a string backwards? How would you do it?"

"Well... I would create a StringBuilder, put a string in it... Then create a new string, and in a loop from the last character to the first... Or maybe a bitwise shift...

"Not bad. But it could be faster. There is a special method for doing this — reverse(); Example:

Code Output
String str = "Hello";
StringBuilder builder = new StringBuilder(str);
String result = builder.toString();

"Besides the StringBuilderclass, Java has another string-like class called StringBuffer. It is an analogue of the StringBuilderclass, but its methods are marked with the synchronized modifier."

"What does that mean?"

"It means that the StringBuffer object can be accessed simultaneously from multiple threads."

"I'm not very familiar with threads yet. But 'can be accessed from multiple threads' is clearly better than 'can't be accessed from multiple threads'... Why not always use StringBuffer then?"

"Because it is much slower than StringBuilder. You may need such a class when you start to actively explore multithreading in the Java Multithreading quest.

"I'll write that to my memory so as not to forget. Marked 'for the future'.

New Java Syntax, level 9, lesson 7
Flipping strings
Using a StringBuilder in the reverseString(String) method, reverse the string that is received as a parameter.