The following table shows the differences between the String and StringBuffer/StringBuilder in Java Technology.
String | StringBuffer/StringBuilder | |
1 | String Object is immutable. | StringBuffer/StringBuilder Object is mutable. |
2 | Immutable means the value stored in the String object can’t be changed. | Mutable means the value stored in the String object can be changed. |
3 | Inefficient | Efficient. |
4 | ‘overloaded mathematical operators’ (‘+’) is used for adding strings which generates string new instances everytime. Example: String st = “Hello”; st = st + “Java”; | ‘append’ is used for adding two or more strings which expands StringBuffer. Example: StringBuilder sb = new StringBuilder(“Hello”); sb.append(“Java”); |
5 | String is not synchronized. Which means it is not thread safe. | StringBuilder/StringBuffer is synchronized. Which is thread safe and hence you can use it when you implement threads for methods. |
6 | Poor performance. | Better Performance. |
7 | Small amount of manipulation can be done. | Large amount or heavy amount of manipulation can be done. |
Related Articles:
Difference between static class loading and dynamic class loading in java
Difference between Array and ArrayList in Java
Difference between Abstract Class and Interface in Java
Difference between HashMap and HashTable in Java
Difference between Csharp and Java
Difference between ArrayList and Vector in Java
Difference between Method Overloading and Method Overriding in Java
Difference between ‘is-a’ relationship and ‘has-a’ relationship in Java
Difference between C++ and Java
Difference between static class loading and dynamic class loading in java
Difference between Array and ArrayList in Java
Difference between Abstract Class and Interface in Java
Difference between HashMap and HashTable in Java
Difference between Csharp and Java
Difference between ArrayList and Vector in Java
Difference between Method Overloading and Method Overriding in Java
Difference between ‘is-a’ relationship and ‘has-a’ relationship in Java
Difference between C++ and Java