Differences between String, StringBuilder and StringBuffer (Understanding and understanding deeper about String in Java)

Tram Ho

n this article, I will talk more about String, StringBuilder and StringBuffer. You will better understand and know when to use them, as well as how to use them properly, how to save memory, … I still use some English words interlocking so that you know What to talk about, after searching google for easy, but translated into Vietnamese because I also read Vietnamese documents and then want to search many times when using the word key is not standard to be out.


String in Java is immutable ie cannot be changed. When you change a String Object in Java, a completely new String object will be created each time you change it. There are 2 ways to declare a String Object in Java, you can refer to the example below

The difference between these two methods (method) is that when using the “new” operator, a new String Object will be created in the heap memory. But when using a literal object (Literal can be understood in some context as “using pure string”), if this object already exists in the heap memory then it will not create new objects anymore, use reused, helping to save heap memory When using String literals, the String Object is stored somewhere called String Pool in Java (I call it the string pond. It is located inside the heap memory, which saves a lot of space for Java Runtime although it is quite time consuming to create a String Object. So if you want to create a new String Object every time you call it, use the “new” operator, and if you want to save heap memory, then use a literal string to create a String Object.

When using the “new” operator, if the object does not exist in the String Pool, it will first create a String object in the String pool, and then create it in the heap memory. Usually the String object will always refer to the object in the heap memory. However, if it already exists in the String Pool, then only in the String Object heap memory will be created. This is often asked during interviews, guys, please pay attention. In addition, Java String class allows access to many useful methods (methods) to apply to String. Some methods are: substring (), charAt (), length (), equals (), concat (), replace (), trim (), split (), toUpperCase () and toLowerCase () . These methods can be used independently, as well as in combination with each other, depending on your intended use.

Before continuing, I would like to explain through the String Pool and Heap Memory to let you understand better.

In the image above, you can see that Java Heap does not always have a String Pool (that part of silver gray). Here, the String s1 and s2 objects with the value = “Cat” all refer to the “Cat” object in the String Pool. When we use double quotes (2 quotes) to create a String Object, then it will first look in the String Pool to see if any of them have such value, if it is then it takes that guy already. it returns otherwise it creates a new object and stuffs it into the Pool. Using the “new” operator, we will force the String class to create a new String object in the heap memory, we can use the intern () method to put that object into the Pool or refer to another String Object. have the same value in the Pool.

See more at: link


The StringBuilder Class makes String classes more flexible because it can create a set of text strings that can change objects. The StringBuilder Class also has a range of methods that interact or affect String objects like in the String Class.

StringBuilder and StringBuffer provide methods that String Class doesn’t have, such as insert (), delete () and reverse (). When we need to manipulate a lot or the string is too long, we should use StringBuilder or StringBuffer because they are much faster and save more memory than String. For example, if you want to output 0 to 99 in a String, using StringBuilder will be much more convenient.

StringBuilder does not provide synchronization, meaning instances of StringBuilder cannot be shared between threads. In a multithreaded environment, one should use StringBuilder because it is faster than StringBuffer


Like StringBuilder, StringBuffer is also used to create long strings that can be changed easily. Both StringBuilder and StringBuffer use the same methods. The difference between these two is StringBuffer, it is thread-safe (If an object is created and only used inside the method that created it, it is considered thread safe) and synchronized (synchronized). ie this StringBuffer is used in multithreading. In a multithreaded environment , one should use StringBuffer

Here, when threads access the hello () method, StringBuffer must be used to ensure thread-safety. Thank you for reading my article.

Reference source: link

Share the news now

Source : Viblo