Difference Between String , StringBuilder And StringBuffer Classes

 String :

In Java, a String is an object that represents a sequence of characters. The string class is part of the Java Standard Library and provides various methods to work with strings. In Java, strings are immutable, which means once created, their contents cannot be changed.

String Operation:

Initialization :

String str1 = "Hello, World!"; // Using string literal
String str2 = new String("Hello, World!"); // Using the new keyword and constructor

Concatenation :

Simply Concatenation means is to add two things . In Java , for adding two strings we can do the Concatenation operation which is done using +.

String firstName = "John";
String lastName = "Doe";
String fullName1 = firstName + " " + lastName;
String fullName2 = firstName.concat(" ").concat(lastName);

Length:

In Java, to calculate a number of character in a string , .length() method is used. 
String text = "Hello, World!";
int length = text.length();

Character Access

In Java , to access any characters in a string we have to use the CharAt() method . This method have starts from 0 index which resembles as a array .

String str = "Hello";
char firstChar = str.charAt(0); // 'H'
char lastChar = str.charAt(str.length() - 1); // 'o'

Comparision :

In Java , two compare two strings , we have to use equals() method or equalsIgnoreCase() method.

String str1 = "hello";
String str2 = "HELLO";
boolean isEqual = str1.equals(str2); // false
boolean isEqualIgnoreCase = str1.equalsIgnoreCase(str2); // true

StringBuilder

In Java, StringBuilder is a class that provides a mutable sequence of characters. Unlike the regular String class, which is immutable (cannot be changed after creation), StringBuilder allows you to modify the contents of the string without creating new objects. This makes it more efficient for situations where you need to perform a lot of string manipulation.

StringBuilder Operation:

Initialization:

StringBuilder sb = new StringBuilder(); // Creates an empty StringBuilder
StringBuilder sb = new StringBuilder("Hello"); // Creates a StringBuilder with initial value "Hello"

Insert:

In java , if we want to add any character or strings in a specific position within the string builder , then we have to use the insert() method.

StringBuilder sb = new StringBuilder("Hello");
sb.insert(5, ", "); // Result: "Hello, " 
Delete:

In Java, if we want to delete any character or a range of character within the  string builder , then we have to use the delete() method.
StringBuilder sb = new StringBuilder("Hello, World!");
sb.delete(5, 13); // Result: "Hello!"
Replace:

In Java, if we want to replace any character or a range of character within the  string builder  , then we have to use the replace() method.
StringBuilder sb = new StringBuilder("Hello, World!");
sb.replace(0, 5, "Hi"); // Result: "Hi, World!"
String Conversion:

When all the operation is done and we have to show the final string, then we can use the toString() method which will change the StringBuilder to a string.
StringBuilder sb = new StringBuilder("Hello");
sb.append(", ").append("World").append("!");
String result = sb.toString(); // Result: "Hello, World!"
StringBuffer:

In Java, the StringBuffer class is a part of the java.lang package and is used to create mutable sequences of characters, similar to the String class. It allows you to modify the contents of the sequence without creating new objects, making it suitable for situations where string manipulation is required.

StringBuffer Operation:

Initialization:

StringBuffer sb = new StringBuffer(); // Creates an empty StringBuffer
 
Insert: In java , if we want to add any character or strings in a specific position of a StringBuffer , then we have to use the insert() method.

StringBuffer sb = new StringBuffer("Hello");
sb.insert(5, ", "); // Result: "Hello, "

Delete and Replace:

For deleting a characters or a string from a StringBuffer we have to use the delete() and replace() method.
StringBuffer sb = new StringBuffer("Hello, World!");
sb.delete(5, 13); // Result: "Hello!"
sb.replace(0, 5, "Hi"); // Result: "Hi, World!"
 
String Conversion:

For Converting a StringBuffer to a complete string , we have to use toString() method.
StringBuffer sb = new StringBuffer("Hello");
sb.append(", ").append("World").append("!");
String result = sb.toString(); // Result: "Hello, World!"
Difference Between String , StringBuilder And StringBuffer Classes

String: Immutable, cannot be changed after creation. Thread-safe due to immutability.

StringBuilder: Mutable, allows modifications without creating new objects. Not thread-safe.

StringBuffer: Mutable and thread-safe, allows modifications in multi-threaded environments, but slightly less efficient than StringBuilder due to synchronization.


Next Post Previous Post