Wednesday, November 10, 2010

String, StringBuffer & stringBuilder in JAVA

1] Differnces between the above three is:
   String is immutable
   StringBuffer & StringBuilders are mutable.

2] Differnces between the StringBuffer& stringBuiler:
   StringBuffer is synchronized.
   StringBuilder is unsynchronized.

3] On which scenario we have to use the above three?
   String: If the text is not going to change use a string Class because a String object is    immutable.
   StringBuffer: If the text can changes, and will be accessed from multiple threads, use a StringBuffer because StringBuffer is synchronous.
   StringBuilder : If the text can change and will only be accessed from a single thread, then use StringBuilder because StringBuilder is unsynchronized.

1 comment: