Oct 6

Strings are a critical part of the Java language and especially so in a web application. Here are a variety of pointers for dealing with Strings.

Use StringUtils

A lot of the common String operations that aren’t in the JDK library are available in Commons Lang StringUtils. Check out those methods and you’ll find your code looking much cleaner (and you’ll have to write less of it!). Anytime you’re about to write a piece of simple string manipulation code that you think should be in the library, check Commons first.

Concatenating a List<String> or a String[]

Check out StringUtils.join() to see if it’s already done for you! Also, see the discussion below about the different ways to concatenate strings.

Case insensitive comparison

Strings have a .equalsIgnoreCase() method that lets you do what you actually want rather than worrying about case conversions.

Prefer StrBuilder to StringBuffer/StringBuilder

Instead of the JDK StringBu* classes and the associated confusion explained below, try using StrBuilder. Apache Commons has created this very handy class that has a much more powerful and flexible API than StringBuffer. Give it a try and you won’t be able to go back to the regular StringBu* classes.

StringBuffer, StringBuilder and +

Strings in Java are immutable, so to append two Strings together the JVM must create another String. When you do that just once, the performance impact is negligible but if you do it many times (in a loop, or a more complicated piece of code) the cost of all of those intermediate Strings adds up. In order to deal with this problem, the StringBuffer and StringBuilder class were added to the Java core libraries. StringBuffer is threadsafe but slightly slower (because of the synchronization) and StringBuilder is not threadsafe but slightly faster. Both of the builder implementations have the con that they make the code harder to read.

The difference between using StringBu* and the ‘+’ operator is generally trivial, except in loops where it can become a significant performance hit.

As a general guideline, you should use the ‘+’ operator to concatenate simple Strings. The java compiler will actually convert code like:

a = b + c + d;

To look like:

a = new StringBuilder(b).append(c).append(d).toString();

So you get the performance when the code is compiled, and easier readability while the code is still in source. As of 1.5, the compiler can NOT yet optimize away the ‘+=‘ operator.

This also means that if you use + in a loop, then you’re incurring a performance penalty for creating a new StringBuilder in every iteration. They’ll be garbage collected eventually, but its not a good habit to get into. Make sure to declare the buffer/builder outside of the loop:

StringBuilder b = new StringBuilder();
 
for(String: stringList){
    b.append(s);
}
 
return b.toString();

Again, most of the basic use cases where you’d want to do that are actually already implemented for you in Commons Lang StringUtils.

Don’t optimize String concatenations!

This may seem to go against the advice of the rest of this post, but bear with me. Don’t immediately go into your codebase fixing these issues everywhere they crop up. That’s just a waste of time. Use profiling to determine which loops are actually causing slowdowns and fix those. Just keep these tips in mind as you write new code and focus on core readability as a primary requirement.


leave a reply

Spam Protection by WP-SpamFree