String and StringBuffer Rules

These rules deal with different problems that can occur with manipulation of the class String or StringBuffer.

AvoidDuplicateLiterals

Code containing duplicate String literals can usually be improved by declaring the String as a constant field.

This rule is defined by the following Java class: net.sourceforge.pmd.rules.strings.AvoidDuplicateLiteralsRule

Here's an example of code that would trigger this rule:

			

public class Foo {
 private void bar() {
    buz("Howdy");
    buz("Howdy");
    buz("Howdy");
    buz("Howdy");
 }
 private void buz(String x) {}
}

    
		

This rule has the following properties:

NameDefault valueDescription
threshold4The number of duplicate literals reporting threshold

StringInstantiation

Avoid instantiating String objects; this is usually unnecessary.

This rule is defined by the following Java class: net.sourceforge.pmd.rules.strings.StringInstantiation

Here's an example of code that would trigger this rule:

			

public class Foo {
 private String bar = new String("bar"); // just do a String bar = "bar";
}

    
		

StringToString

Avoid calling toString() on String objects; this is unnecessary

This rule is defined by the following Java class: net.sourceforge.pmd.rules.strings.StringToStringRule

Here's an example of code that would trigger this rule:

			

public class Foo {
 private String baz() {
  String bar = "howdy";
  return bar.toString();
 }
}

    
		

InefficientStringBuffering

Avoid concatenating non literals in a StringBuffer constructor or append().

This rule is defined by the following Java class: net.sourceforge.pmd.rules.strings.InefficientStringBuffering

Here's an example of code that would trigger this rule:

			

public class Foo {
 void bar() {
  // Avoid this
  StringBuffer sb=new StringBuffer("tmp = "+System.getProperty("java.io.tmpdir"));
  // use instead something like this
  StringBuffer sb = new StringBuffer("tmp = ");
  sb.append(System.getProperty("java.io.tmpdir"));
 }
}

    
		

UnnecessaryCaseChange

Using equalsIgnoreCase() is faster than using toUpperCase/toLowerCase().equals()

This rule is defined by the following Java class: net.sourceforge.pmd.rules.strings.UnnecessaryCaseChange

Here's an example of code that would trigger this rule:

			
                 
 public class Foo {
  public boolean bar(String buz) {
    // should be buz.equalsIgnoreCase("baz")
    return buz.toUpperCase().equals("baz");
    // another unnecessary toUpperCase()
    // return buz.toUpperCase().equalsIgnoreCase("baz");
  }
 }
                 
       
		

UseStringBufferLength

Use StringBuffer.length() to determine StringBuffer length rather than using StringBuffer.toString().equals("") or StringBuffer.toString().length() ==

This rule is defined by the following Java class: net.sourceforge.pmd.rules.strings.UseStringBufferLength

Here's an example of code that would trigger this rule:

			
  
public class Foo {
 void bar() {
  StringBuffer sb = new StringBuffer();
  // this is bad
  if(sb.toString().equals("")) {}
  // this is good
  if(sb.length() == 0) {}
 }
}

  
      
		

AppendCharacterWithChar

Avoid concatenating characters as strings in StringBuffer.append

This rule is defined by the following Java class: net.sourceforge.pmd.rules.strings.AppendCharacterWithChar

Here's an example of code that would trigger this rule:

			

public class Foo {
 void bar() {
  StringBuffer sb=new StringBuffer();
  // Avoid this
  sb.append("a");

  // use instead something like this
  StringBuffer sb=new StringBuffer();
  sb.append('a');
 }
}

    
		

ConsecutiveLiteralAppends

Consecutively calling StringBuffer.append with String literals

This rule is defined by the following Java class: net.sourceforge.pmd.rules.strings.ConsecutiveLiteralAppends

Here's an example of code that would trigger this rule:

			

public class Foo {
 private void bar() {
   StringBuffer buf = new StringBuffer();
   buf.append("Hello").append(" ").append("World"); //bad
   buf.append("Hello World");//good
 }
}

    
		

This rule has the following properties:

NameDefault valueDescription
threshold1The report threshold

UseIndexOfChar

Use String.indexOf(char) when checking for the index of a single character; it's faster.

This rule is defined by the following Java class: net.sourceforge.pmd.rules.strings.UseIndexOfChar

Here's an example of code that would trigger this rule:

			

public class Foo {
 void bar() {
  String s = "hello world";
  // avoid this
  if (s.indexOf("d") {}
  // instead do this
  if (s.indexOf('d') {}
 }
}

    
		

InefficientEmptyStringCheck

String.trim().length() is an inefficient way to check if a String is really empty, as it creates a new String object just to check its size. Looping through a string, checking Character.isWhitespace() on each character and returning false if a non-whitespace character is found is preferable

This rule is defined by the following Java class: net.sourceforge.pmd.rules.strings.InefficientEmptyStringCheck

Here's an example of code that would trigger this rule:

			

public class Foo {
    void bar(String string) {
        if (string != null && string.trim().size() > 0) { 
		    doSomething();
        } 
    }
}

    
		

InsufficientStringBufferDeclaration

Failing to pre-size a StringBuffer properly could cause it to re-size many times during runtime. This rule checks the characters that are actually passed into StringBuffer.append(), but represents a best guess "worst case" scenario. An empty StringBuffer constructor initializes the object to 16 characters. This default is assumed if the length of the constructor can not be determined.

This rule is defined by the following Java class: net.sourceforge.pmd.rules.strings.InsufficientStringBufferDeclaration

Here's an example of code that would trigger this rule:

			

public class Foo {
    void bar() {
        StringBuffer bad = new StringBuffer();
        bad.append("This is a long string, will exceed the default 16 characters");//bad 
        StringBuffer good = new StringBuffer(41);
        good.append("This is a long string, which is pre-sized");//good
    }
}