Efficient manipulation of strings

The use of strings is an important component of most applications and can contribute to poor performance if not managed correctly. This recipe examines techniques used to improve the use of strings.

Getting ready

String manipulation in Java is supported through three java.lang classes:

  • String An immutable object
  • StringBuilder Performs string manipulation but does not use synchronized methods
  • StringBuffer Performs string manipulation using synchronized methods

Each of these classes has its place. For simple strings that are not changed, the String class is a good choice. If strings are manipulated using operations such as concatenation, StringBuilder and StringBuffer are better choices. However, since StringBuffer ...

Get EJB 3.1 Cookbook now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.