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's the difference between javaString, StringBuilder and StringBuffer?

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

Share

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

This article introduces what is the difference between javaString, StringBuilder and StringBuffer. The content is very detailed. Interested friends can use it for reference. I hope it will be helpful to you.

1. What they have in common:

All three are string classes. Both StringBuilder and StringBuffer inherit the AbstractStringBuilder class, and storage strings are stored using arrays.

two。 Difference:

A. the array used by the String class to store strings has been modified by final. When adding modified characters to the String object, you need to re-new a character array and copy the original value to the new character array, so when the value of the string needs to be modified frequently, you need to consider using StringBuilder or StringBuffer to deal with it.

Public final class String implements java.io.Serializable, Comparable, CharSequence {private final char value [];}

B.StringBuffer and StringBuilder also use character arrays to store strings, but do not use final to decorate them; methods defined by StringBuffer are decorated with synchronized, which means that StringBuffer class methods are thread-safe, while StringBuilder methods are not decorated, and the common method for both is append ().

3. Use the scene:

Consider using String when the string value does not change. When the string value is variable, consider using StringBuilder or StringBuffer. When thread safety is required, consider using StringBuffer.

So much for sharing the differences between javaString, StringBuilder and StringBuffer. I hope the above content can be of some help to you and learn more knowledge. If you think the article is good, you can share it for more people to see.

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