2023-05-03 11:06:20 +02:00
== Why is this an issue?
2023-06-06 15:53:52 +02:00
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`.
2021-04-28 16:49:39 +02:00
2023-06-01 17:47:44 +02:00
Consider simplifying when possible for more efficient and cleaner code.
2021-04-28 18:08:03 +02:00
2023-05-03 11:06:20 +02:00
=== Noncompliant code example
2021-04-28 16:49:39 +02:00
2022-02-04 17:28:24 +01:00
[source,java]
2021-04-28 16:49:39 +02:00
----
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
2023-06-01 17:47:44 +02:00
BigDecimal bigDecimal = new BigDecimal("42.0"); // Compliant (see Exceptions section)
2021-04-28 16:49:39 +02:00
----
2021-04-28 18:08:03 +02:00
2023-05-03 11:06:20 +02:00
=== Compliant solution
2021-04-28 16:49:39 +02:00
2022-02-04 17:28:24 +01:00
[source,java]
2021-04-28 16:49:39 +02:00
----
String empty = "";
String nonempty = "Hello world";
2023-06-01 17:47:44 +02:00
Double myDouble = 1.1;
Integer integer = 1;
Boolean bool = true;
2021-04-28 16:49:39 +02:00
BigInteger bigInteger1 = BigInteger.valueOf(3);
BigInteger bigInteger2 = BigInteger.valueOf(9223372036854775807L);
BigInteger bigInteger3 = new BigInteger("111222333444555666777888999");
2023-06-01 17:47:44 +02:00
BigDecimal bigDecimal = new BigDecimal("42.0");
2021-04-28 16:49:39 +02:00
----
2021-04-28 18:08:03 +02:00
2023-05-03 11:06:20 +02:00
=== Exceptions
2021-04-28 16:49:39 +02:00
2023-06-01 17:47:44 +02:00
`BigDecimal` constructor with `double` argument is ignored as using `valueOf` instead might change resulting value.
See S2111.
== Resources
* https://docs.oracle.com/javase/tutorial/java/data/autoboxing.html[Oracle - Learning the Java Language] - Autoboxing and Unboxing
2021-04-28 18:08:03 +02:00
2021-06-02 20:44:38 +02:00
2021-06-03 09:05:38 +02:00
ifdef::env-github,rspecator-view[]
2021-09-20 15:38:42 +02:00
'''
== Implementation Specification
(visible only on this page)
2023-05-25 14:18:12 +02:00
=== Message
Remove this "xxx" constructor
2021-09-20 15:38:42 +02:00
2021-06-08 15:52:13 +02:00
'''
2021-06-02 20:44:38 +02:00
== Comments And Links
(visible only on this page)
2023-05-25 14:18:12 +02:00
=== 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
2021-06-03 09:05:38 +02:00
endif::env-github,rspecator-view[]