String, StringBuilder, and StringBuffer Do You Know the Difference?

A complete guide for Strings in Java

Manusha Chethiyawardhana
Javarevisited

--

Difference between String StringBuilder and StringBuffer in Java
Photo by Amador Loureiro on Unsplash

All developers deal with words and phrases in some parts of their code. And String is a data type used in programming to represent text.

Java has three classes named String, StringBuilder, and StringBuffer, which have methods written to manipulate Strings. These classes are included in the Java lang package and are imported into our code automatically.

In this article, I will explain more about these classes and discuss how to use them and when to use them to make String manipulation easier.

String

Strings in Java are immutable (cannot be changed). When a change is made to a String object in Java, a completely new String object is created each time. There are two ways to declare a String object in Java, and this is shown in the example below.

The difference between these two methods is that when using the new operator, a new String object will be created each time in the heap…

--

--