String

These checkers deal with different problems that can occur with String manipulation.

DuplicateLiterals

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

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

			

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

    
		

StringReturnIgnore

Detects when the return value of a String method call is not used

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

			

public class StringReturnIgnoreExample {
    public void bar() {
        String x = "Hello";
        x.concat(" too good");
        System.out.println("Hello World!" + x);// /Display the string.
    }
}

    
		

StringInstantiation

Avoid instantiating String objects; this is usually unnecessary.

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

			

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

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

			

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