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:StringBuilder sb = new StringBuilder("Hello, World!");
sb.delete(5, 13); // Result: "Hello!"
Replace: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: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:StringBuffer sb = new StringBuffer("Hello, World!");
sb.delete(5, 13); // Result: "Hello!"
sb.replace(0, 5, "Hi"); // Result: "Hi, World!"
String Conversion:StringBuffer sb = new StringBuffer("Hello");
sb.append(", ").append("World").append("!");
String result = sb.toString(); // Result: "Hello, World!"
Difference Between String , StringBuilder And StringBuffer Classes