What is the difference between String, StringBuilder and StringBuffer?

PointStringStringBuilderStringBuffer
ChangeableNo (immutable)YesYes
Thread safeYes (immutable)NoYes (synchronized)
SpeedSlow for many joinsFastestSlower than StringBuilder
Use forFixed textBuilding text in a loopOld code with many threads
public class Main {
    public static void main(String[] args) {
        StringBuilder sb = new StringBuilder("Java");
        sb.append(" is").append(" fun");
        System.out.println(sb);
        System.out.println(sb.reverse());
    }
}
Output

Java is fun
nuf si avaJ

Details: Java StringBuilder and Text Blocks.


Learn more about the similar topics:
Tutorials
No Content Found.
Exercises & Assignments
No Content Found.
Interview Questions & Answers
No Content Found.