Home  >  Article  >  Java  >  A brief discussion on the differences between String, StringBuffer and StringBuilder

A brief discussion on the differences between String, StringBuffer and StringBuilder

青灯夜游
青灯夜游forward
2019-11-27 17:15:541786browse

In Java development interviews, we are often asked about the differences between String, StringBuider, and StringBuffer. Many people do not answer comprehensively and in-depth enough, so what are the differences between them?

A brief discussion on the differences between String, StringBuffer and StringBuilder

All three are used to operate strings. String is usually used to define a variable, while StringBuilder StringBuffer is usually used to splice strings and other operations. But in fact, String can also be used to splice strings, but why do we rarely use it? Let’s start with the underlying codes of the three.

A brief discussion on the differences between String, StringBuffer and StringBuilder

#The underlying code of String is a char array modified with final, which means that after defining a String variable, the content of the variable is immutable.

A brief discussion on the differences between String, StringBuffer and StringBuilder

A brief discussion on the differences between String, StringBuffer and StringBuilder

A brief discussion on the differences between String, StringBuffer and StringBuilder

StringBuilder and StringBuffer both inherit from AbstractStringBuilder, and the char array of this class does not use final Modification, the content is variable, what does this mean?

For example:

String s1= "a";
String s2 = new String("b") ;
String s3 = s1+s2;

If so, we get the string "ab", because there is no immutable string at the bottom of String, so three objects will be created, all of which will occupy memory, and this The three objects have never lost references, so the jvm cannot be garbage collected, resulting in a large waste of memory resources. This is not recommended in our development, but StringBuilder and StringBuffer do not have such a problem.

A brief discussion on the differences between String, StringBuffer and StringBuilder

A brief discussion on the differences between String, StringBuffer and StringBuilder

A brief discussion on the differences between String, StringBuffer and StringBuilder

Through the underlying code, we can see that StringBuilder and StringBuffer pass Determine whether the length of the string is sufficient, and create a new array to encapsulate the data, while the original object is given up reference and waits for garbage collection, thereby reducing memory waste. Therefore, when splicing strings and other operations, we usually use StringBuilder and StringBuffer, but the execution efficiency of the two is different. StringBuilder has higher execution efficiency. Why is this?

A brief discussion on the differences between String, StringBuffer and StringBuilder

A brief discussion on the differences between String, StringBuffer and StringBuilder

Through the underlying source code, you can find that StringBuffer uses synchronization locks when splicing strings, which improves security, but StringBuilder does not use it. Synchronization lock, so efficiency is improved.

Now you should figure out the differences between the three of them! !

This article comes from the java introduction column, welcome to learn!

The above is the detailed content of A brief discussion on the differences between String, StringBuffer and StringBuilder. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:源码时代. If there is any infringement, please contact admin@php.cn delete