Comparison - String, StringBuilder & StringBuffer
Difference between String, String Builder, and String Buffer
Feature
String
StringBuilder
StringBuffer
// String
String str = "Hello";
str = str.concat(" World"); // Creates a new string "Hello World"
System.out.println(str); // Output: Hello World
// StringBuilder
StringBuilder sb = new StringBuilder("Hello");
sb.append(" World"); // Modifies the existing StringBuilder object
System.out.println(sb.toString()); // Output: Hello World
// StringBuffer
StringBuffer sb = new StringBuffer("Hello");
sb.append(" World"); // Modifies the existing StringBuffer object
System.out.println(sb.toString()); // Output: Hello World Last updated