So sánh string và stringbuilder stringbuffer
Trong bài viết này chúng ta cùng nhau bàn luận về các đặc điểm của String, StringBuilder, StringBuffer để xem chúng có những đặc tính gì và trong trường hợp nào thì chúng ta cần sử dụng chúng. StringString là một chuỗi ký tự immutable vì nó sẽ dễ dàng chia sẽ ra các method hoặc các thread khác nhau vì giá trị của chúng sẽ không đổi sau khi đã được khởi tạo. Khi bạn khởi tạo một String bằng cách sử dụng cặp dấu “” thì việc đầu tiên java sẽ đi tìm trong JVM string pool xem có string nào có giá trị tương tự chưa? nếu có thì trả về reference vừa tìm được ngược lại khởi tạo object string và đưa vào string pool. Cơ chế này giúp JVM giảm thiểu đáng kể vùng nhớ. Nhưng khi thực hiện các hành động như cộng chuỗi, cắt chuỗi thì chúng sẽ khởi tạo một object mới thay vì sửa đổi trên giá trị cũ. String vs StringBufferBởi vì String có tính chất immutable nên khi khi thực hiện các hành động như nối chuỗi, cắt chuỗi thì nó sẽ khởi tạo một object string mới và loaị bỏ giá trị cũ cho trình dọn rác của Java xử lý. Nếu chúng ta xử lý quá nhiều hành động trên String thì nó sẽ sinh ra rất nhiều giá trị rác ở heap. Vì vậy nên java cung cấp StringBuffer class sử dụng trong trường hợp chúng ta cần thao tác nhiều trên chuỗi. StringBuffer và StringBuilder đều là mutable object cung cấp sẵn cho chúng ta các method append(), insert(), delete() etc. StringBuider vs StringBufferVề mặt cơ bản thì StringBuider và StringBuffer là hoàn toàn giống nhau và đều là mutable object. Điểm khác biệt duy nhất giữa chúng là StringBuffer cung cấp cơ chế đồng bộ thread safe nhung giảm hiệu năng trong khi StringBuilder thực hiện không đồng bộ hiệu năng cao hơn. String is one of the most widely used classes in Java. StringBuffer and StringBuilder classes provide methods to manipulate strings. We will look into the difference between StringBuffer and StringBuilder. StringBuffer vs StringBuilder is a popular Java interview question. The string is one of the most important topics in the core java interview. If you are writing a program that prints something on the console, you are use String. This tutorial is aimed to focus on major features of String class. Then we will compare the StringBuffer and StringBuilder classes.
Since String is immutable in Java, whenever we do String manipulation like concatenation, substring, etc. it generates a new String and discards the older String for garbage collection. These are heavy operations and generate a lot of garbage in heap. So Java has provided StringBuffer and StringBuilder classes that should be used for String manipulation. StringBuffer and StringBuilder are mutable objects in Java. They provide append(), insert(), delete(), and substring() methods for String manipulation. StringBuffer was the only choice for String manipulation until Java 1.4. But, it has one disadvantage that all of its public methods are synchronized. StringBuffer provides Thread safety but at a performance cost. In most of the scenarios, we don’t use String in a multithreaded environment. So Java 1.5 introduced a new class StringBuilder, which is similar to StringBuffer except for thread-safety and synchronization. StringBuffer has some extra methods such as substring, length, capacity, trimToSize, etc. However, these are not required since you have all these present in String too. That’s why these methods were never implemented in the StringBuilder class. StringBuffer was introduced in Java 1.0 whereas StringBuilder class was introduced in Java 1.5 after looking at shortcomings of StringBuffer. If you are in a single-threaded environment or don’t care about thread safety, you should use StringBuilder. Otherwise, use StringBuffer for thread-safe operations. I am trying to check the effect on performance because of synchronization with a sample program that performs
I ran the same code for the StringBuffer object also to check the time and memory values. I have executed the code 5 times for each case and then calculated the average values. Value of i StringBuffer (Time, Memory) StringBuilder (Time, Memory) 10,00,000 808, 149356704 633, 149356704 1,00,00,000 7448, 147783888 6179, 147783888 It’s clear that StringBuilder performs better than StringBuffer even in the case of a single-threaded environment. This difference in performance can be caused by synchronization in StringBuffer methods.
That’s all for a quick roundup of difference between String, StringBuffer, and StringBuilder. StringBuilder is better suited than StringBuffer in most of the general programming scenarios. References: |