Skip to main content

String vs StringBuffer vs StringBuilder

String is immutable whereas StringBuffer and StringBuilder are mutable classes.

String is one of the most widely used classes in Java. StringBuffer and StringBuilder classes provide methods to manipulate strings.


Since String is immutable in Java,whenever we do String manipulation like concatenation, sub-string, etc.it generates a new String.So Java has provided StringBuffer and StringBuilder classes that should be used for String manipulation.

StringBuffer vs StringBuilder

StringBuffers all public methods are synchronized.

StringBuffer provides Thread safety but at a performance cost , it reduce performance.

In most of the scenarios, we don’t use String in a multithreaded environment.

So Java 1.5 introduced a new class StringBuilder, 

which is similar to StringBuffer except for thread-safety and synchronization.

StringBuffer has some extra methods such as substring, length, capacity, trimToSize, etc.

However,these are not required since you have all these present in String too.

That’s why these methods were never implemented in the StringBuilder class.

StringBuffer was introduced in Java 1.0 whereas StringBuilder class was introduced in Java 1.5 after looking at shortcomings of StringBuffer.


StringBuffer and StringBuilder are mutable objects in Java and they provide append(), insert(), delete(), and substring() methods for String manipulation.

StringBuffer is thread-safe and synchronized whereas StringBuilder is not.That’s why StringBuilder is faster than StringBuffer.

For String manipulations in a non-multi threaded environment, we should use StringBuilder , and in multithreaded environment always use StringBuffer class.

StringBuilder performs better than StringBuffer even in the case of a single-threaded environment. This difference in performance can be caused by synchronization in StringBuffer methods.

If you are in a single-threaded environment or don’t care about thread safety, you should use StringBuilder. Otherwise, use StringBuffer for thread-safe operations. 

check link String in java

check program on this link : click here to understand program

Happy Learning , Have a great day ahead

Popular Posts

Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.

Today we will see solution for Error In Spring Boot : ***************************  APPLICATION FAILED TO START ***************************  Description: Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.  Reason: Failed to determine a suitable driver class. --->My Application.yml : server: port: 8081 spring: datasource: driver-class-name: oracle.jdbc.driver.OracleDriver url: jdbc:oracle:thin:@localhost:1521:xe username: sys as sysdba password: root Oracle Version is 21 C Oracle Database 21c Express Edition Release 21.0.0.0.0 - Production Version 21.3.0.0.0. check my pom.xml here : <? xml version = " 1.0 " encoding = " UTF-8 " ?> < project xmlns = " http://maven.apache.org/POM/4.0.0 " xmlns:xsi = " http://www.w3.org/2001/XMLSchema-instance " xsi:schemaLocation = " http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/m...

Stream In Java

stream in java 8 : Stream API is used to process collection of objects. stream() method is present in Collection interface from package java.util . Stream is an interface present in java.util package, filter() and map() methods are present in Stream interface. -->when to use filter()  method ? If we want to filter elements from collection based on some boolean condition then we can use filter() method.  for example  : filtering even or odd numbers from list of numbers. -->When to use map()  method ? If we want to create new object for every object in collection then we should use map() method. for example : if we want to increase every number in list by 2 , here every object is getting modified ,we have to use map() in this type of scenario. In below example , we used both filter() and map() methods on stream , inside filter method we applied some condition and in map method we are transforming our object to different one. package com.crtr4u.www; import java...

Class and Object in Java

In Java we have to always deal with objects, now let's understand what is object , how to define object in Java. What is Object In Java ? Object is an entity with STATE and Behavior. Examples of Objects : Bike ,Mobile, Student,Laptop,Car etc. If we consider Bike As Object , Bike Name is state , Bike Color is state , and Running is behavior of Bike,Starting is behavior of bike and stopping is also behavior of Bike. In Java we say , Object is an Instance of class , it means we can create object using class. What is class ? A class is template or blueprint from which objects are created. A class in java can contain fields(variables),Methods(Behaviors),blocks,constructors etc. How To Create class in Java ? Syntax To Create Class :      class College {           String collegeName="COEP";           public void getAdmission(){           System.out.println("In this method we have to wri...