Managing strings
The class String
is used a lot. So, you have to have a good handle on its functionality. We talked already talked about String
value immutability in Chapter 5, Java Language Elements and Types. We have shown that every time a String
value is "modified", a new copy of the value is created, which means that in the case of multiple "modifications", many String
objects are created, consuming memory and putting a burden on the JVM.
In such cases, it is advisable to use the class java.lang.StringBuilder
or java.lang.StringBuffer
because they are modifiable objects and do not have an overhead of creating String
value copies. We will show how to use them and explain the difference between these two classes in the first part of this section.
After that, we will review the methods of the class String
and then provide an overview of the class org.apache.commons.lang3.StringUtils
, which complements the class String
functionality.
StringBuilder and StringBuffer
The classes StringBuilder...