StringBuilder class in Java

StringBuilder class in Java

Are strings in java really immutable? Let's have a look into it in this article.

In this article, I will try to cover the basics of the StringBuilder class and its various properties.

This post was originally posted on Showwcase

Introduction

Before we jump into the topic, let us take an example. Let's suppose there is the word "Showwcase" and we want to append the word "App" to it. The most common and natural way to do this is by using the concatenation (+) symbol. That is "Showwcase" + "App" which gives the string "ShowwcaseApp".

But how does Java do this internally? 👇

img

Every time you try to modify a string, a new string object is created in the memory rather than modifying the original object. Here, while we try to append the string "App" to "Showwcase", a new reference for the string "ShowwcaseApp" is created in heap memory.

The reason this is happening is that the Strings in Java are "immutable". This is because of security, caching, and performance reasons. If you want to read more about the reason for immutability, click here.

We can cheat a little bit and make the strings to be mutable. That's where the concept of StringBuilder comes into the picture.

StringBuilder class

Let us consider the same example of concatenating "Showwcase" and "App" using a string builder object.

class Demo{
        public static void main(String[] args){
          StringBuilder sb = new StringBuilder("Showwcase");
          str.append("App"); 
          System.out.println(str); 
        }
}

Output:

ShowwcaseApp

Here the StringBuilder object sb is used. When the word "App" is appended to the sb object, rather than creating a new object, the same sb object is modified in the heap memory.

img

Methods of StringBuilder class

1. append()

It is used to add a new sequence to the end of a particular string.

StringBuilder s1 = new StringBuilder("Hello");
s1.append("World"); 
System.out.println(s1);

Output:

HelloWorld

2. charAt()

This method is used for getting a character at a particular index in the StringBuilder object.

 StringBuilder s2 = new StringBuilder("Hey"); 
    System.out.println(s2.charAt(0));
//prints the character at the index 0 of the string s2;

Output:

H

3. reverse()

This method is used to reverse the string object.

    StringBuilder s3 = new StringBuilder("Blog");
    s3.reverse();
    System.out.println(s3);

Output:

golB

4. indexOf()

Returns the index within this string of the first occurrence of the specified substring. If the index doesn't exist, -1 will be returned.

    StringBuilder s4 = new StringBuilder("Showwcase");
    System.out.println(s4.indexOf("w"));

Output:

3 
// since the first occurrence of 'w' is at the index 3

Pros and cons of using the StringBuilder class

It can boost the performance of string operations since it does not create different objects for modifying a particular string, hence reducing the wastage of memory.

But along with this, there is a disadvantage to StringBuilder too. Since the StringBuilder object is mutable, it is not thread-safe. That is to say, it is not synchronized which may lead to multiple threads accessing the StringBuilder object at the same time.

Also, an important thing to note is that if you are not modifying the string too often, then it is not advisable to use StringBuilder since it takes more time to instantiate and it uses more buffer memory to store.

Conclusion

Thank you for reading till the end! I hope this article has been helpful for you to understand how the StringBuilder class works and the various methods used by it. If you found this article helpful, then please like and share. If you have any questions or feedback, do leave it in the comment section below.

Have a great day!

References

Oracle Docs

Kunal-Youtube

Geeks for Geeks