String, StringBuffer, StringBuilder

Shubham Dadasaheb Sawant
3 min readJun 25, 2022

A. String:

  1. String is immutable.
  2. Immutable means the object from which string is created that object cannot be modified or changed.
  3. If we try to modify that object the new object is created with those changes.

OUTPUT: java

B. StringBuffer

  1. is mutable and sychronized.
  2. Synchronized means only one thread can access the methods of string buffer at a time.
  3. less efficient than StringBuilder
  4. If thread saftey is important always prefer stringbuffer.

OUTPUT: java language works!!

Performance test between StringBuffer and StringBuilder

So from above snapshot you can see that StringBuilder is faster than StringBuffer.

If need to convert
1. String to StringBuffer or StringBuilder

2. StringBuffer and StringBuilder to String:

OUTPUT: JAVA Programming

3. StringBuffer to StringBuilder and vice-versa:

OUTPUT: JAVA PROGRAMMING

SCP:
1. String constant pool
2. Previously a separate area apart from heap, stack scp area was used to store string objects.
3. Now this area is moved inside of heap and scp is expandable in heap area

Advantages of SCP area

OUTPUT: All variables are referencing to same object.

equals() methods in StringBuffer and StringBuilder is used to compare the objects address and equals() in String is used to check the content of variables is equal or not.

--

--