Developer

Two tips for handling strings in Java

When it comes to managing strings, you can greatly improve your Java programs simply by rethinking your approach. These tips on string comparisons and concatenation will help make your programs stand out.


By Stewart Buskirk

Sometimes, it’s the little things that make all the difference in a program’s performance. That’s the case with the two Java tips below. The first tip shows that by carefully considering the data you will be using, you can fashion a string comparison that won’t cause an exception if you have a null value. In the second tip, you will see how logic that worked great for procedural programming can cause your Java program to really take a hit when it comes to manipulating strings.

Avoid null String comparisons
What's wrong with this picture?
private final String aConstant = "testvalue";

boolean isEqualToConstant(String myParameter)
{
      return myParameter.equals(aConstant);
}


Most Java programmers get caught in this construct at some point; the problem is that if the myParameter variable is null, the comparison will throw a NullPointerException. While we can always catch exceptions or test for null values before calling such functions, there's a better way to improve our function:
return aConstant.equals(myParameter);
Simply by exchanging the two String objects in the comparison line, we can guarantee that our method will never throw a NullPointerException because the equals method will always be called on a non-null String. The same rule applies for inline constants or other String comparison methods:
return "testvalue".equalsIgnoreCase(myParameter);


If the myParameter String is null, the comparison will simply return false.

So if you are ever comparing two Strings and you know one will be non-null, always pass the unknown String as the method parameter, and you'll never see those exceptions again.

Use StringBuffer when appending to Strings
In Java, Strings are immutable objects; that is, they cannot be modified in any way after they are created. This means that when you attempt to modify a String object, you are often doing much more work than you might think.

For example, here is a simple and common method of concatenating values in Java:
String aString = "";
aString = aString+"a";
aString = aString+"b";
System.out.println(aString);


Because Strings are immutable, the old aString object has to be discarded and a new aString object created every time a new String is appended. In terms of both execution time and memory consumption, object creation can be one of the most costly types of operations in Java.

Compare that to the StringBuffer approach:
StringBuffer aBuffer = new StringBuffer();
aBuffer.append("a");
aBuffer.append("b");
System.out.println(aBuffer.toString());


By using a StringBuffer object, the appended strings are added directly to the internal byte array of the StringBuffer rather than causing a new String object to be allocated.

In general, if you find yourself appending values to a String more than once in the lifetime of the String object, using a StringBuffer to manage your changing values can improve the speed and efficiency of your Java code.

Editor's Picks