
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
Difference between String buffer and String builder in Java
String buffer and StringBuilder both are mutable classes which can be used to do operation on string objects such as reverse of string, concating string and etc. We can modify string without creating a new object of the string. A string buffer is thread-safe whereas string builder is not thread-safe. Therefore, it is faster than a string buffer. Also, a string concat + operator internally uses StringBuffer or StringBuilder class. Below are the differences.
Sr. No. | Key | String Buffer | String Builder |
---|---|---|---|
1 | Basic | StringBuffer was introduced with the initial release of Java | It was introduced in Java 5 |
2 | Synchronized | It is synchronized | It is not synchronized |
3 | Performance | It is thread-safe. So, multiple threads can’t access at the same time, therefore, it is slow. | It is not thread-safe hence faster than String Buffer |
4 | Mutable | It is mutable. We can modify string without creating an object | It is also mutable |
5 | Storage | Heap | Heap |
Example of StringBuilder
public class StringBuilderExample{ public static void main(String[] args){ StringBuilder builder=new StringBuilder("Hi"); builder.append("Java 8"); System.out.println("StringBuilderExample" +builder); } }
Example of StringBuffer
public class StringBufferExample{ public static void main(String[] args){ StringBuffer buffer=new StringBuffer("Hi"); buffer.append("Java 8"); System.out.println("StringBufferExample" +buffer); } }
- Related Articles
- Difference between string and StringBuffer in Java.
- How to calculate String Buffer capacity in Java?
- Difference between String and Character array in Java.
- Difference Between String and StringBuffer Class in Java
- Difference between String class and StringBuffer class in Java
- What is the difference between String s1 = "Hello" and String s1= new String("Hello") in java?
- Difference between Buffer and Cache
- Difference between Cache and Buffer
- What is the difference between a String object and a String literal in Java?
- How to use string Builder class in android?
- How to use string buffer in android?
- Convert buffer to readable string in JavaScript?
- What is the difference between String and string in C#?
- Difference between String and StringBuffer.
- Difference between String and StringBuilder in C#

Advertisements