"Boxing" is the process of putting a primitive value into a primitive-wrapper object. When that’s done purely to use the wrapper class'
toString
method, it’s a waste of memory and cycles because those methods are static
, and can therefore be used without a
class instance. Similarly, using the static
method valueOf
in the primitive-wrapper classes with a non-String
argument should be avoided.
Noncompliant code example
int myInt = 4;
String myIntString = (new Integer(myInt)).toString(); // Noncompliant; creates & discards an Integer object
myIntString = Integer.valueOf(myInt).toString(); // Noncompliant
Compliant solution
int myInt = 4;
String myIntString = Integer.toString(myInt);