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
Java Strings
Java String Methods
Java StringBuilder and Text Blocks
Exercises & Assignments
Reverse a string
Check if a string is a palindrome
Count the vowels in a string
Interview Questions & Answers
What is the difference between == and equals() in Java?
Why is String immutable in Java?
What is the difference between String, StringBuilder and StringBuffer?
What is the string pool in Java and what does intern() do?