When writing an application in Java, there are several things to look out for. What is the optimization consideration? Speed
or Memory? This begs the question, when do you use
StringBuilder you’re reserving space to add strings to your buffer. When you’re using a
String, every time you
type something like this:
you’re throwing away your existing
s string and creating a new one.
This means that you’re constantly needing new memory to make the concatenated string, throw the old one out, and put the reference of your variable to your new string. To put this in perspective refer to this SO question.
StringBuilder you can append to your string without having to remove the existing part.
Consider this example:
<Large File> on lines 50 and 69 with the path to a large file and run the sample. After running this snippet, you would
String method consumed more memory. On the other hand
StringBuilder finished processing faster.
String class represents character strings. All string literals in Java programs, such as
"abc", are implemented as
instances of this class.
Strings are constant (immutable); their values cannot be changed after they are created. String buffers support mutable
strings. Because String objects are immutable they can be shared.
The Java language provides special support for the string concatenation operator ( + ), and for conversion of other objects to strings.
String concatenation is implemented through the
StringBuffer) class and its append method.
conversions are implemented through the method toString, defined by
Object and inherited by all classes in Java.
StringBuilder objects are like
String objects, except that they can be modified (mutable). Internally, these objects
are treated like variable-length arrays that contain a sequence of characters. At any point, the length and content of
the sequence can be changed through method invocations.
Strings should always be used unless string builders offer an advantage in terms of simpler code or better performance.
For example, if you need to concatenate a large number of strings, appending to a StringBuilder object is more efficient.
StringBuilder class, like the
String class, has a
length() method that returns the length of the character sequence
in the builder.
Unlike strings, every string builder also has a capacity, the number of character spaces that have been allocated. The capacity, which is returned by the
capacity() method, is always greater than or equal to the length (usually greater than)
and will automatically expand as necessary to accommodate additions to the string builder.
Maven is used to manage the dependencies of this
Back to the question raised at the beginning of this post. Which would you consider? It depends. When you are writing reactively
fast applications and speed is a concern, you may consider
StringBuilder. On the other hand when memory is a major constraint and
GC overhead is an issue then
String may very well suit your needs. This does not however lock you to use one or the other. Feel
free using both in the same application wherever they are best suited. Until the next post, keep doing cool things .