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.
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
String myNum = "42.0"; float f = Float.parseFloat(myNum); int myInteger = Integer.parseInt(myNum);