StringBuffer class is used to create mutable (modifiable) String objects. The StringBuffer class in Java is the same as String class except it is mutable i.e. it can be changed. StringBuffer class is thread-safe i.e. multiple threads cannot access it simultaneously. So it is safe and will result in an order.
String |
StringBuffer |
|
1) |
The String class is immutable. |
The StringBuffer class is mutable, meaning you can modify the content of the string without creating a new object. |
2) |
String is slow and consumes more memory when we concatenate
too many strings because every time it creates new instance. |
StringBuffer is fast and consumes less memory when we
concatenate strings. |
3) |
String class overrides the equals() method of Object class. So
you can compare the contents of two strings by equals() method. |
StringBuffer class doesn't override the equals() method of
Object class. |
4) |
String class is slower while performing concatenation
operation. |
StringBuffer class is faster while performing concatenation
operation. |
5) |
String class uses String constant pool. |
StringBuffer uses Heap memory. |
StringBuffer has an initial capacity, and it automatically expands its capacity if needed when you append or insert characters. You can also set the initial capacity explicitly using a constructor. The default capacity of the buffer is 16.If the number of character increases from its current capacity, it increases the capacity by (oldcapacity*2)+2. For example if your current capacity is 16, it will be (16*2)+2=34.
Комментариев нет:
Отправить комментарий