Table of Contents
In this article, we will look into how StringBuffer class is different from StringBuilder Class in Java. A StringBuffer Class is same as String Class with only exceptions that it is mutable which means it can be changed while String Class is Immutable as it cannot be changed. On the other hand if we talk about StringBuilder Class then it is primarily created to replace the StringBuffer Class in places where single thread needs to call the StringBuffer Class.
What is Immutable
Any object which does not change its state is known as Immutable. Since it cannot change its state so it cannot be edited or changed or corrupted by any other thread. String Class in Java can be the best example for this.
What is Mutable
Any object whose state can be changed is known as Mutable. By default all the objects in Java(except strings which are immutable) are Mutable. Any programmers who wants to change or set some value, they need to use mutable objects.
How StringBuffer Class is different from StringBuilder Class in Java
Also Read: Best Steps to Install Growpart Command and Resize root partition in Linux(RHEL/CentOS 7/8)
StringBuffer Class
- It is thread safe class.
- It is synchronized which means two different threads cannot call the
StringBuffer
Class simultaneously. - It is mutable which means it changes its value.
- It is introduced in
JDK 1.0.
- It extends
java.lang.Object
Class. - Some of the important methods of this class are
append()
andinsert()
methods which are overloaded methods. StringBuffer
implementsSerializable, Appendable and CharSequence.
- It is slower as it provides synchronization. Check more about
StringBuffer
on Java Official Page.
Program Example 1
[root@localhost ~]# vi Example.java import java.io.*; class Example { public static void main(String[] args) { StringBuffer sb = new StringBuffer("CyberITHub"); sb.insert(5,"hello"); System.out.println(sb); } }
Compile Your Program
[root@localhost ~]# javac Example.java
Run Your Program
[root@localhost ~]# java Example CyberhelloITHub
Program Example 2
[root@localhost ~]# vi Example.java import java.io.*; class Example { public static void main(String[] args) { StringBuffer sb = new StringBuffer("CyberITHub"); sb.append("hello"); System.out.println(sb); } }
Compile Your Program
[root@localhost ~]# javac Example.java
Run Your Program
[root@localhost ~]# java Example CyberITHubhello
Program Example 3
[root@localhost ~]# vi Example.java import java.io.*; class Example { public static void main(String[] args) { StringBuffer sb = new StringBuffer("CyberITHub"); sb.reverse(); System.out.println(sb); } }
Compile Your Program
[root@localhost ~]# javac Example.java
Run Your Program
[root@localhost ~]# java Example buHTIrebyC
Program Example 4
[root@localhost ~]# vi Example.java import java.io.*; class Example { public static void main(String[] args) { StringBuffer sb = new StringBuffer("CyberITHub"); int slen = sb.length(); System.out.println(slen); } }
Compile Your Program
[root@localhost ~]# javac Example.java
Run Your Program
[root@localhost ~]# java Example 10
StringBuilder Class
- It is not a thread safe class.
- It is non-synchronized which means two different threads can call the
StringBuilder
Class simultaneously. - It is mutable which means it changes its value.
- It is introduced in
JDK 1.5
. - It extends
java.lang.Object
Class. - Some of the important methods are
append()
andinsert()
methods which are overloaded methods. StringBuilder
implementsSerializable, Appendable and CharSequence
.- It is faster as it does not provide any synchronization. Check More about
StringBuilder
on Java Official Page.
Program Example 1
[root@localhost ~]# vi Example.java import java.io.*; class Example { public static void main(String[] args) { StringBuilder sb = new StringBuilder("CyberITHub"); sb.insert(5,"hello"); System.out.println(sb); } }
Compile Your Program
[root@localhost ~]# javac Example.java
Run Your Program
[root@localhost ~]# java Example CyberhelloITHub
Program Example 2
[root@localhost ~]# vi Example.java import java.io.*; class Example { public static void main(String[] args) { StringBuilder sb = new StringBuilder("CyberITHub"); sb.append("hello"); System.out.println(sb); } }
Compile Your Program
[root@localhost ~]# javac Example.java
Run Your Program
[root@localhost ~]# java Example CyberITHubhello
Program Example 3
[root@localhost ~]# vi Example.java import java.io.*; class Example { public static void main(String[] args) { StringBuilder sb = new StringBuilder("CyberITHub"); sb.reverse(); System.out.println(sb); } }
Compile Your Program
[root@localhost ~]# javac Example.java
Run Your Program
[root@localhost ~]# java Example buHTIrebyC
Program Example 4
[root@localhost ~]# vi Example.java import java.io.*; class Example { public static void main(String[] args) { StringBuilder sb = new StringBuilder("CyberITHub"); int slen = sb.length(); System.out.println(slen); } }
Compile Your Program
[root@localhost ~]# javac Example.java
Run Your Program
[root@localhost ~]# java Example 10
Popular Recommendations:-
50 Useful zypper command examples to Manage Packages on OpenSUSE Linux
How to Start and Enable SSHD Service in OpenSUSE Linux
How to Start / Stop / Restart Network Service in OpenSUSE Linux
How to Check Stateful and Stateless Pods in Kubernetes Cluster
3 Easy Ways to Check/Find OpenSUSE Linux Version
6 Easy Steps to Setup and Manage Log rotation Using logrotate in Linux
Migrate CentOS 8 to CentOS Stream 8 in 6 Easy Steps
26 iostat, vmstat and mpstat command examples to Monitor Linux Performance