"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 |
---|---|
|
Converts the passed object to a string and appends it to the current string |
|
Converts the passed object to a string and inserts at the middle of the current string |
|
Replaces the part of the string specified by the start..end interval with the passed string |
|
Removes the character with the specified index from the string |
|
Removes characters within the specified interval from the string |
|
Searches for a substring in the current string |
|
Searches for a substring in the current string, starting from the end |
|
Returns the character in the string at the passed index |
|
Returns the substring defined by the specified interval |
|
Reverses the current string. |
|
Changes the character at the specified index to the passed character |
|
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 |
---|---|
|
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 |
---|---|
|
Hi123 |
How do I delete a character?
"To delete a character in a mutable string, you need to use the deleteCharAt()
method. Example:
Code | Output |
---|---|
|
Helo |
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 |
---|---|
|
MeHello!w |
"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 |
---|---|
|
olleH |
"Besides the StringBuilder
class, Java has another string-like class called StringBuffer
. It is an analogue of the StringBuilder
class, 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'.