Java: String Vs StringBuffer Vs StringBuilder

String in Java

A String class represents an array of characters.

String Instance Creation
String instance can be created in two ways:

  • By assigning as Literals
    String title = "Facing Issues On IT";
  • By using the new keyword
    String title = new ("Facing Issues On IT");

Points to Remember for Java String

  • The string class is immutable in Java, so it’s easy to share it across different threads or functions.
  • When you create a String using double quotes, it first looks for the String with the same value in the JVM string pool, if match found it returns the reference else it creates the String object and then places it in the JVM String pool. This way JVM saves a lot of space by using the same String in different threads. But if a new operator is used, it will always explicitly creates a new String in the heap memory.
  • + operator overloading is used to concatenating two strings. Although internally it uses StringBuffer to perform this action.
  • String overrides equals() and hashCode() methods, two Strings are equal only if they have the same characters in the same order. Note that equals() method is case sensitive, so if you are not looking for case sensitive checks, you should use equalsIgnoreCase() method.
  • String value represents a string in the UTF-16 format.
  • String is a final/immutable class with all the fields as final except “private int hash”. This field contains the hashCode() function value and created only when the hashCode() method is called and then cached in this field. Furthermore, the hash is generated using the final fields of String class with some calculations, so every time hashCode() method is called, it will result in the same output. For the caller, it’s like calculations are happening every time but internally it’s cached in the hash field.

Why StringBuffer & StringBuilder?

The string class is immutable in java i.e whenever we do any manipulation in String like concatenation, substring, reverse, etc. always generate a new string and discard older String for garbage collection.

That’s the reason only Java introduced StringBuffer in JDK 1.4 and StringBuilder in JDK 1.5.

StringBuffer and StringBuilder are mutable objects and provide append(), insert(), delete() and substring() methods for String manipulation.

StringBuffer vs StringBuilder

Apart from similarities, Java StringBuffer and StringBuilder having differences:

  • StringBuffer is thread-safe because all of its methods are synchronized but the main disadvantage is performance.

Note: If you are working on a single-threaded environment go with StringBuilder and in a multithreaded environment use StringBuffer. In general scenarios for string manipulation, StringBuilder is better suited than StringBuffer because String buffer is synchronized.

String vs StringBuffer vs StringBuilder

String StringBuffer StringBuilder
Immutable Mutable Mutable
Legacy JDK 1.4 JDK 1.5
Thread Safe Thread Safe No Thread Safe
Synchronized Synchronized Not Synchronized
Performance slow in manipulation Performance slow in manipulation Performance faster in manipulation
String Concat (+) uses StringBuffer and StringBuilder internally NA NA
See Also: String Class Examples See Also: StringBuffer Examples See Also: StringBuilder Examples