Wednesday, January 30, 2013

String, StringBuffer & StringBuilder

Most important interview question right :). Let us have a single line use case for each:

String: If the text is not going to change use a String Class because it is immutable.

StringBuffer: If the text can changes, and will be accessed from multiple threads, use StringBuffer because it is synchronous.

StringBuilder: (Available from Java 5): If the text can change and will only be accessed from a single thead, use StringBuilder as it is immutable and unsynchronized.