CodesJava

Easy learning with example program codes

StringBuffer in java


StringBuffer is a class in java whose object represents the mutable string. It is just like string class except that its object can be modified. StringBuffer is synchronized, hence it is thread-safe.

i.e. StringBuffer class objects are thread safe , mutable sequence of characters.

For these reasons, Java would handle an expression like

String newString = aString + anInt + aChar + aDouble;

like this:

String newString = (new StringBuffer(aString)).append(anInt).append(aChar).append(aDouble).toString();

Note: StringBuffer not override equals method of object class, so StringBuffer objects should be converted into string objects if you want to compare StringBuffer class objects.

Constructors of StringBuffer class.

1. public StringBuffer():creates a string buffer with no characters and with default capacity 16 i.e. it can contain 16 characters.

2. public StringBuffer(int capacity): creates a string buffer with no characters and with specified capacity.

Note: capacity should not be less than zero , otherwise it will throw NegativeArraySizeException.

3. public StringBuffer(String str):creates a string buffer with specified string as initial content.

Initial capacity = Default capacity + length of the string.

Note:
1. string should not be null otherwise it will throw NullPointerException.
2. if string length is equal to zero than string buffer with no content and default capacity is return.

4. public StringBuffer(CharSequence charSquence):creates a string buffer with specified character sequence as its initial content.

Note:
1. charSquence should not be null otherwise it will throw NullPointerException.
2. if charSquence length is equal to zero than string buffer with no content and default capacity is return.

Commonly used methods of StringBuffer class.

S.No. Method Description
 1. append(String str) Append the specified string at the end of this string.
 2. insert(int offset, String str) Insert specified string at the offset indicated position.
 3. replace(int startIndex, int endIndex, String str) Replace the substring of the string buffer from startIndex to endIndex-1 with specified string.
 4. delete(int startIndex, int endIndex) delete the substring of the string buffer from startIndex to endIndex-1.
 5. reverse() replace the string buffer’s character sequence by reverse character sequence.
 6. capacity() returns the current capacity of string buffer. Capacity refers to the amount of available storage.
 7. ensureCapacity(int minCapacity) ensures that the capacity is at least equal to the specified minimum.

 
Next Topic: append(String str) StringBuffer method in java.
Previous Topic: trim() String functions in java with example.

Related Topics:

StringBuffer in java.
append(String str) StringBuffer method in java.
insert(int offset, String str) StringBuffer method in java.
replace(int startIndex, int endIndex, String str) StringBuffer method in java.
delete(int startIndex, int endIndex) StringBuffer method in java.
reverse() StringBuffer method in java.
capacity() StringBuffer method in java.
ensureCapacity(int minCapacity) StringBuffer method in java.
StringTokenizer in java.

 

Posted in Java   

Core Java Tutorial

Programming language overview.
Overview of Java.
Java features
JVM architecture details.
JVM, JRE and JDK.
Java Coding Guidelines.
Some important definitions.
Variable and data types.
Hello world java program.
Core java examples programs.
Important Java Programs.
How to set permanent path in java?
OOPs Basics.
Object and Class in Java.
OOPs Principles/Concepts.
Abstraction in java.
Encapsulation in java.
Polymorphism in java.
Method overloading in java.
Method overriding in java.
Dynamic method dispatch.
Runtime polymorphism.
Association in java.
Inheritance in java.
Aggregation in java.
Command line arguments in java.
Command line argument program in eclipse.
Read input from command line using Scanner.
Java array programs
Java star pattern programs
Java number pattern programs
final in java.
Abstract class in java.
Interface in java.
Custom marker Interface in java.
Constructor in java.
Package in java.
Access modifier in java.
Static import in java.
Package class in java.
this in java.
Instance initialize block.
Anonymous block.
super in java.
Static in java.
final in java.
Java cloning deep and shallow
Shallow vs Clone copy
String handling in java.
String handling programs.
StringBuffer in java.
StringBuilder in java.
Exception Handling Tutorial.
Multithreading Tutoial.
Java input output stream tutorial.
Collections framework in java tutorial.
Collections framework programs.
Java Random class
Java annotations.
Java design principles.
Java 7 features
Java 8 features
Java networking tutorial
Java Reflection tutorial
Sign Up/ Sign In
Ask a Question


Industrial Training

We offers Placement Oriented Training on Java, Spring, JSF, Hibernate, PHP, AngularJS, Angular 4, PLSQL, Oracle BI Publisher etc. We also provides Online training, please mail us at hr@codesjava.com.

Development

We also provides the Development services for Website Development , Java Development, PHP Development, Android App Development etc. You can contact us on hr@codesjava.com.

Copyright © 2018 CodesJava DMCA.com Protection Status SiteMap Reference: Java Wiki