Difference between String and Stringbuffer in Java
Difference between String and Stringbuffer in Java
String | Stringbuffer |
---|---|
String is immutable. | It is mutable. |
It is slow in terms of executing the concatenation task. | It is fast in terms of executing the concatenation task. |
Here the length of the string class is static. | Here the length can be modified whenever required, as it is dynamic in behaviour. |
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. |
String class uses String constant pool. | StringBuffer uses Heap memory. |
String consumes more as compared to the stringbuffer. | StringBuffer uses less memory as compared to the string. |
It utilises a string constant pool to store the values. | It prefers heap memory to store the objects. |
It overrides both equal() and hashcode() techniques of object class. | It cannot override equal() and hashcode() methods. |