preface

To see is to surrender, which is to say that if you can see something, you can get it.

To me recording is seeing.

The interview questions are not just for the interview, but to help us understand what we already know more deeply.

The difference between the String and StringBuffer/StringBuilder

  • A String is an immutable String
  • StringBuffer/StringBuilder is variable string

StringBuffer/StringBuilder is variable string

StringBuffer/StringBuilder is variable string that should be well understood, because when we create a StringBuffer/StringBuilder objects can append () method to change the content of the object.

StringBuilder sb=new StringBuilder("abc");
sb.append("efg");
Copy the code

In the above code, the content of the sb reference to the memory address is ABC. After calling append, the content of the sb reference to the memory address is abcdefg.

A String is an immutable String

Here’s why strings are immutable. You can also check out the reference link at the bottom of this article. I’ll summarize it here as I understand it.

There are two main reasons why String objects are immutable:

1. The String class attribute of the modifier is not final is private, which means the String does not provide similar like StringBuffer/StringBuilder append method to modify the String object has been created.

2.String is final. We can’t inherit from String, so we can’t add methods like AppEnd to create strings.

The difference between StringBuffer and StringBuilder

In short, StringBuffer is thread-safe, while StringBuilder is non-thread-safe.

Reference:

Juejin. Cn/post / 684490… Juejin. Cn/post / 684490…