What is a Buffer in Java?
A Buffer is a portion in the memory that is used to store a stream of data from peripheral devices. Then from this buffer this stream of data is collected and stored in variables. A stream can be defined as a continuous flow of data. The buffer is quite useful as Java deals everything as a String.
What is String in Java?
Strings in Java are Objects that are backed internally by a char array. Since arrays are immutable(cannot grow), Strings are immutable as well. Whenever a change to a String is made, an entirely new String is created.
Characteristics of Java String
- Java string object is immutable i.e you can’t change the value of string after it created.
- String class is slower while performing concatenation operation.
- String class overrides the equals() method of Object class. So you can compare the contents of two strings by equals() method.
- String class uses String constant pool.
- String is slow and consumes more memory when we concatenate too many strings because every time it creates new instance.
Characteristics of Java StringBuffer
- Java StringBuffer is mutable i.e after creating StringBuffer you can change the value of StringBuffer.
- StringBuffer class is faster while performing concatenation operation.
- StringBuffer class does not override the equals() method of Object class.
- StringBuffer uses Heap memory.
- StringBuffer is fast and consumes less memory when we concatenate the strings.
Why is String final in Java?
Why String is Immutable or Final in Java. The string is Immutable in Java because String objects are cached in String pool. At the same time, String was made final so that no one can compromise invariant of String class e.g. Immutability, Caching, hashcode calculation etc by extending and overriding behaviors.
How is StringBuffer implemented?
StringBuffer/StringBuilder is implemented in a much more efficient way by creating an array of all the strings and copying them back to a string only when necessary. Essentially skipping the x + 2x + 3x until (n-1)x. The only copy operation it needs to perform occurs when the string is ready to be copied over.
Can StringBuffer be converted to String?
A StringBuffer object can be converted to String using toString() method of Object class. In the above code, equals() method is to be applied on String objects but not on StringBuffer objects; that is, sb1. euqals(sb2) raises error. So, to compare two StringBuffer objects, it is required to convert them into strings.
Also Read: == Operator vs equals ()
String vs StringBuffer
|Basis of Comparison
|Slower than StringBuffer while performing concatenation
|Faster than String while performing concatenation
|Can be increased
|Usage in threaded environment
|Not used in a threaded environment
|Used in a multi-threaded environment
String variable= new String(“String”);
|StringBuffer variable= new StringBuffer(“String”);
Also Read: Difference Between Throw And Throws
StringBuffer Vs StringBuilder
Since String is immutable in Java, whenever we do String manipulation like concatenation, substring, etc. it generates a new String and discards the older String for garbage collection.
These are heavy operations and generate a lot of garbage in heap. So Java has provided StringBuffer and StringBuilder classes that should be used for String manipulation.
StringBuffer was the only choice for String manipulation until Java 1.4. But, it has one disadvantage that all of its public methods are synchronized. StringBuffer provides Thread safety but at a performance cost.
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 and StringBuilder are mutable objects in Java. They provide append(), insert(), delete(), and substring() methods for String manipulation.
The primary difference between StringBuffer and StringBuilder is that StringBuffer is thread safe while StringBuilder isn’t. This means that if different threads are accessing/modifying the same instance of a StringBuffer, their access is synchronized such that the result is always predictable. Also because StringBuilder is designed for use by single thread, operations on it is faster than that of StringBuffer, otherwise they have mostly identical methods.