
- 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
How can we compare a StringBuilder with StringBuffer in Java?
The StringBuffer objects are generally safe to use in a multi-threaded environment where multiple threads may be trying to access the same StringBuffer object at the same time. The StringBuilder is a replacement for the thread-safe StringBuffer class and it works much faster as it has no synchronized methods. If we are doing lots of String operations in a single thread, we can gain a lot of performance when using this class.
Example
public class CompareBuilderwithBufferTest { public static void main(String []args) { stringBufferTest(); stringBuilderTest(); } public static void stringBufferTest() { long startTime = System.nanoTime(); StringBuffer sb = new StringBuffer(); for (int i=0; i < 1000; i++) { sb.append((char) 'a'); } System.out.println("StringBuffer test: " + (System.nanoTime() - startTime)); } public static void stringBuilderTest() { long startTime = System.nanoTime(); StringBuilder sb = new StringBuilder(); for (int i=0; i < 1000; i++) { sb.append((char) 'a'); } System.out.println("StringBuilder test: " + (System.nanoTime() - startTime)); } }
Output
StringBuffer test: 192595 StringBuilder test: 85733
- Related Articles
- Difference between StringBuilder and StringBuffer in Java
- Java String, StringBuffer and StringBuilder Tutorial.
- Where to use a StringBuffer/StringBuilder than a String in Java?
- What is the difference between a StringBuffer and StringBuilder in java?
- What is the difference between StringBuffer and StringBuilder in java?
- Difference between StringBuffer and StringBuilder.
- How can we replace specific part of String and StringBuffer in Java?
- How can I clear or empty a StringBuilder in Java.
- How to understand StringBuffer is thread-safe and StringBuilder is non-thread-safe in Java?\n
- What is the difference between String, StringBuffer and StringBuilder classes in Java explain briefly?
- How do we compare String in Java
- Can we compare Akshay Kumar with Jackie Chan?
- How do we compare two arrays in Java?
- How can we compare data in two MySQL tables?
- Why should we use a StringBuffer instead of a String in Java?\n

Advertisements