83 lines
2.7 KiB
Plaintext
83 lines
2.7 KiB
Plaintext
== Why is this an issue?
|
|
|
|
Calling constructors for `String`, `BigInteger`, `BigDecimal` and the objects used to wrap primitives is less efficient and less clear than relying on autoboxing or `valueOf`.
|
|
|
|
Consider simplifying when possible for more efficient and cleaner code.
|
|
|
|
=== Noncompliant code example
|
|
|
|
[source,java,diff-id=1,diff-type=noncompliant]
|
|
----
|
|
String empty = new String(); // Noncompliant; yields essentially "", so just use that.
|
|
String nonempty = new String("Hello world"); // Noncompliant
|
|
Double myDouble = new Double(1.1); // Noncompliant; use valueOf
|
|
Integer integer = new Integer(1); // Noncompliant
|
|
Boolean bool = new Boolean(true); // Noncompliant
|
|
BigInteger bigInteger1 = new BigInteger("3"); // Noncompliant
|
|
BigInteger bigInteger2 = new BigInteger("9223372036854775807"); // Noncompliant
|
|
BigInteger bigInteger3 = new BigInteger("111222333444555666777888999"); // Compliant, greater than Long.MAX_VALUE
|
|
BigDecimal bigDecimal = new BigDecimal("42.0"); // Compliant (see Exceptions section)
|
|
----
|
|
|
|
|
|
=== Compliant solution
|
|
|
|
[source,java,diff-id=1,diff-type=compliant]
|
|
----
|
|
String empty = "";
|
|
String nonempty = "Hello world";
|
|
Double myDouble = 1.1;
|
|
Integer integer = 1;
|
|
Boolean bool = true;
|
|
BigInteger bigInteger1 = BigInteger.valueOf(3);
|
|
BigInteger bigInteger2 = BigInteger.valueOf(9223372036854775807L);
|
|
BigInteger bigInteger3 = new BigInteger("111222333444555666777888999");
|
|
BigDecimal bigDecimal = new BigDecimal("42.0");
|
|
----
|
|
|
|
|
|
=== Exceptions
|
|
|
|
`BigDecimal` constructor with a `double` argument is ignored as using `valueOf` instead might change the resulting value.
|
|
See S2111.
|
|
|
|
|
|
== Resources
|
|
|
|
* https://docs.oracle.com/javase/tutorial/java/data/autoboxing.html[Oracle - Learning the Java Language] - Autoboxing and Unboxing
|
|
|
|
|
|
ifdef::env-github,rspecator-view[]
|
|
|
|
'''
|
|
== Implementation Specification
|
|
(visible only on this page)
|
|
|
|
=== Message
|
|
|
|
Remove this "xxx" constructor
|
|
|
|
|
|
'''
|
|
== Comments And Links
|
|
(visible only on this page)
|
|
|
|
=== relates to: S1533
|
|
|
|
=== on 10 Oct 2014, 14:03:24 Freddy Mallet wrote:
|
|
@Ann, my 2 cents:
|
|
|
|
* I would not activate this rule by default
|
|
* Would slightly update the title like this : Constructors should not be used to instanciate new String or primitive wrappers.
|
|
|
|
=== on 13 Oct 2014, 12:40:40 Ann Campbell wrote:
|
|
What do you think [~nicolas.peru]? Active by default or not?
|
|
|
|
=== on 13 Oct 2014, 12:52:34 Nicolas Peru wrote:
|
|
IMO : Activated by default but with a low severity : minor to trivial this is really easy to fix and should not clutter you too much to focus on real trouble but should still be reported to be dealt with.
|
|
|
|
=== on 14 Jul 2016, 16:08:20 Ann Campbell wrote:
|
|
https://github.com/google/error-prone/blob/master/docs/bugpattern/BoxedPrimitiveConstructor.md
|
|
|
|
endif::env-github,rspecator-view[]
|