58 lines
1.5 KiB
Plaintext
58 lines
1.5 KiB
Plaintext
== Why is this an issue?
|
|
|
|
Rather than creating a boxed primitive from a `String` to extract the primitive value, use the relevant `parse` method instead.
|
|
Using `parse` makes the code more efficient and the intent of the developer clearer.
|
|
|
|
|
|
=== Noncompliant code example
|
|
|
|
[source,java,diff-id=1,diff-type=noncompliant]
|
|
----
|
|
String myNum = "42.0";
|
|
float myFloat = new Float(myNum); // Noncompliant
|
|
float myFloatValue = (new Float(myNum)).floatValue(); // Noncompliant
|
|
int myInteger = Integer.valueOf(myNum); // Noncompliant
|
|
int myIntegerValue = Integer.valueOf(myNum).intValue(); // Noncompliant
|
|
----
|
|
|
|
|
|
=== Compliant solution
|
|
|
|
[source,java,diff-id=1,diff-type=compliant]
|
|
----
|
|
String myNum = "42.0";
|
|
float f = Float.parseFloat(myNum);
|
|
int myInteger = Integer.parseInt(myNum);
|
|
----
|
|
|
|
|
|
|
|
ifdef::env-github,rspecator-view[]
|
|
|
|
'''
|
|
== Implementation Specification
|
|
(visible only on this page)
|
|
|
|
=== Message
|
|
|
|
Use "XXX.parseYyy" for this string-to-yyy conversion.
|
|
|
|
|
|
'''
|
|
== Comments And Links
|
|
(visible only on this page)
|
|
|
|
=== on 21 Nov 2024, 16:48:00 Alban Auzeill wrote:
|
|
[test-code-support-investigation-for-java] Decision for scope: Main -> All.
|
|
|
|
=== on 10 Oct 2014, 13:58:53 Freddy Mallet wrote:
|
|
@Ann, for me this RSPEC fully duplicates RSPEC-2131, what's the difference ? Thanks
|
|
|
|
=== on 10 Oct 2014, 17:55:39 Ann Campbell wrote:
|
|
\[~freddy.mallet] they go in opposite directions. This one is string-to-primitive. The other is primitive-to-string. S'okay?
|
|
|
|
=== on 11 Oct 2014, 12:13:20 Freddy Mallet wrote:
|
|
Ok [~ann.campbell.2] !
|
|
|
|
endif::env-github,rspecator-view[]
|