Java String, StringBuffer and StringBuilder Tutorial.



The String class of the java.lang package represents character strings. All string literals in Java programs, such as "ABC", are implemented as instances of this class. Strings are constant, their values cannot be changed after they are created.

The StringBuffer and StringBuilder classes are used when there is a necessity to make a lot of modifications to Strings of characters.

Unlike Strings, objects of type StringBuffer and String builder can be modified over and over again without leaving behind a lot of new unused objects.

The StringBuilder class was introduced as of Java 5 and the main difference between the StringBuffer and StringBuilder is that StringBuilder’s methods do not thread safe (not synchronized).

It is recommended to use StringBuilder whenever possible because it is faster than StringBuffer. However, if the thread safety is necessary, the best option is StringBuffer objects.


Advertisements