Constructors of primitive wrapper objects (e.g.
new Boolean(true) will be
deprecated in Java 9. The
valueOf factory methods
Boolean.valueOf(true)) should always be preferred. Those methods are
called implicitly by autoboxing, which is often more convenient than an
Integer x = Integer.valueOf(23); and
Integer x = 23; are
The explicit constructors always return a fresh instance, resulting
in unnecessary allocations. The
valueOf methods return cached
instances for frequently requested values, offering significantly better space
and time performance.
Relying on the unique reference identity of the instances returned by the explicit constructors is extremely bad practice. Primitives should always be treated as identity-less value types, even in their boxed representations.
Suppress false positives by adding an
@SuppressWarnings("BoxedPrimitiveConstructor") annotation to the enclosing element.