Creating temporary primitive wrapper objects only for String
conversion or the use of the compareTo()
method is
inefficient.
Instead, the static toString()
or compare()
method of the primitive wrapper class should be used.
Noncompliant code example
private int isZero(int value){
return Integer.valueOf(value).compareTo(0); // Noncompliant
}
private String convert(int value){
return Integer.valueOf(value).toString(); // Noncompliant
}
Compliant solution
private int isZero(int value){
return Integer.compare(value, 0); // Compliant
}
private String convert(int value){
return Integer.toString(value); // Compliant
}