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 String StringBuffer and StringBuilder in java

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

Share

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

This article mainly introduces "what is the difference between String StringBuffer and StringBuilder in java". In daily operation, I believe that many people have doubts about the difference between String StringBuffer and StringBuilder in java. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the question of "what is the difference between String StringBuffer and StringBuilder in java?" Next, please follow the editor to study!

In terms of the definition of declaration

Only String can directly declare and create

StringBuffer and StringBuilder must go to the new object.

This is because only String will create a string constant pool in this way of declaration, while others do not.

StringBuffer stf = new StringBuffer ("abc"); StringBuilder stb = new StringBuilder ("abc")

Both StringBuffer and StringBuilder inherit from the abstract class AbstractStringBuilder

From the structural point of view

Once String is created, it is fixed and immutable, and StringBuffer and StringBuilder are also called variable length strings, so why are they variable in length?

The bottom of the string is a char [] array, so let's look directly at the jdk source code

String

StringBuffer

StringBuilder

It can be found that only the underlying char [] array of string is modified with the final keyword, which means that it is a constant, and once we have given a value in the constructor, it is immutable.

But the underlying array of StingBuffer and StringBuilder is not modified with final, which means it is modifiable, for example, as follows

StringBuffer stf = new StringBuffer ("abc"); StringBuffer s = stf.append (stf)

We use the append () method to append and change the value of stf, and then use a new reference to point to it, so do stf and s point to the same object?

Yes, because we only changed the value of its underlying char [] array, where the new reference s still points to the original object

From the perspective of thread safety

Thread safety problems generally exist in the concurrency of multiple threads (multiple threads compete for the same resource)

String is thread safe, because its underlying char [] array is final, it is immutable, so there is no thread safety problem, but because it is immutable, we need to create new ones frequently, which leads to its performance is not very high, so if we want to frequently modify the value of the string in the operation, we usually use the other two.

StringBuilder is not thread safe because it is mutable and can be modified arbitrarily between threads, so there will inevitably be some problems between them, so we usually use it when we need to modify the value of a string frequently in a single thread.

StringBuffer is thread-safe, although it is also mutable, but every method used to operate on it adds the synchronized keyword (synchronous lock), that is, when one thread is operating on the StringBuffer, the other thread has no right to operate until the previous thread releases the lock, which is suitable for multithreading.

When we learn the methods in the StringBuffer class, we need to pay special attention to this method: substring ()

The return type of this method is String, which does not change the value of the original StringBuffer object, for example:

StringBuffer stf = new StringBuffer ("abc"); String substring = stf.substring (1); System.out.println (stf); System.out.println (substring)

The output shows that stf: "abc" subString: "bc" does not change the value of StringBuffer at this time.

At this point, the study on "what is the difference between String StringBuffer and StringBuilder in java" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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