String is an immutable class, so you cannot make changes on a String after creating it. All methods that do an operation on String returns a new one, subString(), toLowercase() etc. You can reinitialize it though, but this creates a new String in memory (as it should do).
This helps in many cases, most of the times you do not want to alter a String (in methods etc.). Unless you need to build a long text by appending new strings to a String. In this situation, you need to use StringBuilder or StringBuffer for memory's sake. These classes are mutable so a new String is not created on every append.
The main difference between StringBuilder and StringBuffer is thread safety.
StringBuffer is synchronized, StringBuilder is not.
StringBuilder is faster than StringBuffer.
This helps in many cases, most of the times you do not want to alter a String (in methods etc.). Unless you need to build a long text by appending new strings to a String. In this situation, you need to use StringBuilder or StringBuffer for memory's sake. These classes are mutable so a new String is not created on every append.
The main difference between StringBuilder and StringBuffer is thread safety.
StringBuffer is synchronized, StringBuilder is not.
StringBuilder is faster than StringBuffer.
This is really a nice blog which completely differentiate between string, string buffer and string builder used in Java programming. Without these, Java coding is not completed. So this blog is well defined the terms mentioned above.
ReplyDelete