JavaScript is not currently enabled, but is required for full CodeSonar manual search and browse functionality.
If you are viewing this file in your hub's Web GUI, enable JavaScript in your browser: you will also need it for GUI functionality.
If you opened this file directly from disk, your browser may be directly suppressing JavaScript functionality: certain browsers perform this suppression on local files (but not files delivered by web servers) for security reasons.
| CodeSonar® 9.2p0 | CONFIDENTIAL | CodeSecure Inc |
A box/unbox sequence can be simplified and made more efficient.
It is often convenient to use a literal or a unique object instead of explicitly constructing new instances, when the same object is used many times, since distinct objects are created at each new statement, which affects the performance of the code. Moreover, objects used instead of literals are often short-lived and consequently must be claimed back from the garbage-collector.
| Class Name | Inefficient Box-Unbox (Java) |
|---|---|
| Significance | reliability |
| Mnemonic | JAVA.CLASS.BUB |
| Categories | None |
| Availability | Available for Java and Kotlin. |
| Enabling | Checks for this warning class are enabled by
default. To disable them, add the following WARNING_FILTER rule to the
project configuration file.
WARNING_FILTER += discard class="Inefficient Box-Unbox (Java)" |
public class Converter {
public static boolean conversion(String stringToboolean) {
return Boolean.valueOf(stringToboolean).booleanValue(); // "Inefficient Box-Unbox (Java)" warning issued here
}
}
The same operation can be performed more efficiently using the method Boolean.parseBoolean, which is used by the implementation of Boolean.valueOf.
The programmer could modify the program to address the problem as follows.
public class Converter {
public static boolean conversion(String stringToboolean) {
return Boolean.parseBoolean(stringToboolean);
}
}
Use string literals instead of the creation of constant strings. Use factory methods or the automatic boxing mechanism instead of the instantiation of a primitive wrapper class.
The following configuration file parameters affect checks for this warning class.
To report problems with this documentation, please visit https://support.codesecure.com/.