Java Reference
In-Depth Information
Applicability
Improper conversions between integers and floating-point values can yield unexpected
results,especiallyfromprecisionloss.Insomecases,theseunexpectedresultscaninvolve
overflow or other exceptional conditions.
It is acceptable to perform operations using a mix of integer and floating-point values
when deliberately exploiting the properties of integer arithmetic before conversion to
floating-point. For example, use of integer arithmetic eliminates the need to use the
floor() method. Any such code must be clearly documented to help future maintainers
understand that this behavior is intentional.
Bibliography
[JLS 2013]
§5.1.2, “Widening Primitive Conversion”
[Long 2012]
NUM13-J. Avoid loss of precision when converting primitive integers to
floating-point
NUM00-J. Detect or prevent integer overflow
61. Ensure that the clone() method calls super.clone()
Cloning a subclass of a nonfinal class which defines a clone() method that fails to call
super.clone() will produce an object of the wrong class.
The Java API [API 2013] for the clone() method says:
By convention, the returned object should be obtained by calling super.clone . If a
class and all of its superclasses (except Object ) obey this convention, it will be the
case that x.clone().getClass() == x.getClass() .
Noncompliant Code Example
Inthisnoncompliantcodeexample,the clone() methodintheclass Base failstocall su-
per.clone() :
Click here to view code image
class Base implements Cloneable {
public Object clone() throws CloneNotSupportedException {
return new Base();
}
 
Search WWH ::




Custom Search