Remove Boxing for String Conversions

Properties

Property Value
First seen in jSparrow version 1.0.0
Minimum Java version 1.1
Remediation cost 5 min
Links S2131

Description

When calling toString() on a boxed primitive no new instance of that primitive has to be created. This rule replaces occurrences of such code with a static method.

Benefits

This rule has performance benefits. As described above boxing the object simply to use the static toString method is a waste of memory and CPU cycles.

Requirement & Tags

Requirements

None

Code Changes

Boolean

Pre

new Boolean(true).toString();

Post

Boolean.toString(true);

Byte

Pre

new Byte(1).toString();

Post

Byte.toString(1);

Double

Pre

new Double(1.0).toString();

Post

Double.toString(1.0);

Float

Pre

new Float(1.0).toString();

Post

Float.toString(1.0);

Integer

Pre

new Integer(1).toString();

Post

Integer.toString(1);

Long

Pre

new Long(1).toString();

Post

Long.toString(1);

Short

Pre

new Short(1).toString();

Post

Short.toString(1);

Automatic Application of This Rule

The automatic application of this rule is supported in the following jSparrow versions: