
- 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
Why should we use a StringBuffer instead of a String in Java?n
- A StringBuffer is a thread-safe, mutable sequence of characters.
- Unlike a String class (immutable), the StringBuffer class is mutable. That is, we can change the contents of a StringBuffer object.
- When we modify a string of StringBuffer class, we are not creating a new String object, but rather operating directly on the original string itself.
- For this reason, the StringBuffer class offers a different set of methods than the String class, all of which operate directly on the buffer that contains the string.
A StringBuffer can be defined simply by the use of the new operator and bypassing the string value inside a StringBuffer constructor.
Example
class StringBufferDemo{ public static void main(String arg[]){ StringBuffer sb = new StringBuffer(); sb.append("Java Tutorials Point"); System.out.println(sb); } }
In the above program, we initially created an instance of a StringBuffer class and appended "Java Tutorials Point" to the StringBuffer class using append() method.
Output
Java Tutorials Point
- Related Articles
- Why we should use whole string in Java regular expression
- Where to use a StringBuffer/StringBuilder than a String in Java?
- Why do we use WebDriver instead of Selenium IDE?
- Why should eval be avoided in Bash, and what should I use instead?
- Why you should use NumPy arrays instead of nested Python lists?
- Why do we need a copy constructor and when should we use a copy constructor in Java?
- Why should we use element in JavaScript?
- Why we should use set.seed in R?
- Why should we use a semicolon after every function in JavaScript?
- How can we replace specific part of String and StringBuffer in Java?
- Why should we not use ++, -- operators in JavaScript?
- Why should we use MySQL CASE Statement?
- Java program to insert a String into a Substring of StringBuffer
- How can we compare a StringBuilder with StringBuffer in Java?
- Why StringBuffer is mutable in Java?

Advertisements