Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the difference between Sring, StringBuffer and StringBuilder in Java

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Most people do not understand the knowledge points of this article "what is the difference between Sring and StringBuffer and StringBuilder in Java", so the editor summarizes the following content to you, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this article "what is the difference between Sring and StringBuffer and StringBuilder in Java".

Variability

String classes usually use character arrays to hold strings, such as Private, final, char, and value [], so we say that string objects are immutable.

Both Stringbuffer and StringBuider inherit from the AbstractStringBuilder class, and AbstractStringBuilder also uses an array of characters to hold strings, char [] value, both of which are mutable.

Performance

When the String type is changed, a new String object is generated, and the pointer is then pointed at the new String object. StringBuffer operates on the StringBuffer object itself, rather than generating a new object and changing the object's reference. In the same case, you can only get a 10%-15% performance improvement by using StringBuilder compared with using StringBuffer, and you have to take the risk that multithreading is not safe.

Thread safety

The objects in String are immutable, and the above is all the contents of the collection of common Linux commands brought to you by Xiaobian. The solution is constant and thread safe. AbstractStringBuilder, as the common parent of StringBuilder and StringBuffer, defines the basic operations of related strings, such as expandCapacity, append, insert, indexOf and other public methods. StringBuffer adds a synchronization lock to the method or a synchronization lock to the called method, so the thread is safe. StringBuilder does not add a synchronization lock to the method, so it is not thread-safe.

A summary of the three

String: to manipulate a small amount of data

StringBuilder: single thread operates on string buffers and manipulates large amounts of data

StringBuffer: multithreading operates on string buffers and large amounts of data

The above is about the content of this article entitled "what is the difference between Sring and StringBuffer and StringBuilder in Java?" I believe we all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report