Last Updated: 22 January, 2023
String and StringBuffer are final classes of the java.lang package and are used to store a sequence of characters.
There are many differences between the String and StringBuffer that are given below.
A String is an immutable and final class in Java. It was introduced in the JDK 1.0 version and defined in the java.lang package.
Java String is an object that allows us to store a sequence of characters. A string object may contain any type of data, such as alphanumeric, special characters, blank spaces, etc., values enclosed in double quotes.
For example, "Core Java" is a string containing a sequence of characters 'C', 'o', 'r', 'e', 'J', 'a', 'v' and 'a'.
Strings are immutable in Java which means their values cannot be changed once they are created; if we modify the string, the JVM will create a new object.
String Class Description
public final class String
extends Object
implements Serializable, Comparable<String>, CharSequence
A Java String literal is created by using double quotes.
For example:
String str = “Hello Java”;
The "new" keyword is used to create a Java String.
For example:
String str = new String(“Hello Java”);
Output
str1 value: Hello
str2 value: Hello Java
str3 value: Hello
s1 value: Java World
s2 value: Hello Java
When we create a String by literal, the object is always created inside the String pool, whereas if we create a string object using the 'new' keyword, it will be created inside the heap memory area.
StringBuffer is a mutable, final, and synchronized (thread-safe) Java class that was introduced in the JDK 1.0 version and is available in the java.lang package.
The StringBuffer class is used to create a mutable String object (the object can be changed once created). StringBuffer is similar to the String class except for one major difference: StringBuffer is mutable, whereas String is immutable (an object cannot be changed once it is created).
The StringBuffer class is thread-safe, which means that at any time, multiple threads cannot access it simultaneously. So it is safe and will result in order, but implementing the synchronization mechanism will take some time for the JVM, which is why StringBuffer is a little slower than StringBuilder.
Java StringBuffer Class Declaration
public final class StringBuffer
extends Object
implements Serializable, Comparable <StringBuffer>, CharSequence
Output
Welcome in StringBuffer Class
String | StringBuffer |
---|---|
A string is an immutable class, which means its values cannot be changed once created. If we modify the string, the JVM will create another object. | A StringBuffer is a mutable class. An object can be changed once it is created. |
Strings can be created in two ways: 1. by string literal 2. by using a new keyword | StringBuffer can be created only one way: by a new keyword. |
In concatenation operations, String is slow and consumes more memory because it creates a new instance every time. | whereas StringBuffer is fast and consumes less memory. |
String uses a String Constant Pool and Heap Memory for storing the string object. | The StringBuffer only uses the heap memory. |
There is no capacity concept in strings. | StringBuffer has a default capacity of 16. |
That's all guys, hope this Java article is helpful for you.
Happy Learning... 😀
feedback@javabytechie.com