Java StringBuilder

Java StringBuilder is identical to StringBuffer except for one important difference it is not synchronized , that means it is not thread safe. The main difference between StringBuffer and StringBuilder is StringBuffer is synchronized, but StringBuilder is not. You should prefer StringBuilder in all cases where you have only a single thread accessing your object.

The important methods in StringBuilder Class are append() , insert() , replace() , delete() , reverse() , and capacity() . The following Java program illustrate the important methods in StringBuilder Class.



StringBuilder append() method

The principal operations on a StringBuilder are the append method, which are overloaded so as to accept data of any type. String appends become increasingly expensive as strings become longer and more memory is required. For example:

In the above case, you should use a StringBuilder instead of a String, because it is much faster and consumes less memory.

If you have a single statement,

then you can use Strings, because the compiler will use StringBuilder automatically.

Mutability Difference of String and StringBuilder

String is immutable , if you try to alter their values, another object gets created, whereas StringBuilder are mutable so they can change their values.
 (C) 2021    Founded by raps mk
All Rights Reserved. All other trademarks are property of their respective owners.
SiteMap  | Terms  | About