String vs StringBuilder vs StringBuffer

[ad_1]

Strings are immutable objects which means once String object is created, it can never be change. Whenever there is some change in String’s value. A new object of String is created in heap.String str=”hello”;
String str=new String(“hello”);

StringBuffers are mutable objects.StringBuffer are continuous sequence of characters. StringBuffers are Thread Safe.

StringBuffer str=new StringBuffer(“zulqarnain”);

StringBuilders are mutable objects. StringBuilders are not Thread Safe
.
StringBuilder str=new StringBuilder(“hello”);

Why and when Using StringBuffer and StringBuilder

StringBuffer and StringBuilder should be used when lot of modification requires in string characters .
If continuous changes required in string character and using String  object you are making waist of memory (String pool objects).

[ad_2]

Leave a Comment