GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||||||||||||||||
GeezerVison is showing www.geeksforgeeks.org — String vs StringBuilder vs StringBuffer in Java - GeeksforGeeks String vs StringBuilder vs StringBuffer in JavaLast Updated : 16 Oct, 2026 In Java, String, StringBuilder, and StringBuffer are used to store and manipulate sequences of characters. The main difference between them lies in mutability, thread safety, and performance. String is immutable, while StringBuilder and StringBuffer are mutable, making them suitable for different string manipulation requirements.
StringA String in Java is an immutable sequence of characters. Once a String object is created, its contents cannot be modified. Operations such as concat(), replace(), and substring() return a string result instead of changing the original object.
Output Hello Explanation: Here, str.concat(" World") does not modify the original string. Instead, it creates a new String object, and the old one remains unchanged. StringBuilderStringBuilder is a mutable sequence of characters introduced in Java 5. Unlike String, it allows modifications to the existing object using methods such as append(), insert(), delete(), and reverse(). StringBuilder is not synchronized, making it generally suitable for repeated string modifications when external synchronization is not required.
Output Hello World Explanation: In the above example append() method modifies the existing object directly without creating a new one. StringBufferStringBuffer is a mutable sequence of characters similar to StringBuilder. However, its main methods are synchronized, allowing individual operations to be performed safely when multiple threads access the same object. This synchronization introduces some overhead compared with StringBuilder.
Output Hello World Explanation: In the above example append() modifies the existing object safely in a multi-threaded environment. Use Cases of String, StringBuilder, and StringBufferGeeks now you must be wondering when to use which one, do refer below as follows:
StringBuilder vs String vs StringBufferThe table below demonstrates the difference between String, StringBuilder and StringBuffer:
Comment Explore | ||||||||||||||||||||
| You are visitor 01402520 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |