All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.sonar.l10n.java.rules.java.S2131.html Maven / Gradle / Ivy

This rule is deprecated; use {rule:java:S1158} instead.

Why is this an issue?

"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);




© 2015 - 2025 Weber Informatics LLC | Privacy Policy