An issue is raised when an integer value (int or long) is implicitly converted to a floating-point type (float or double) in a context where precision loss is possible.

Why is this an issue?

Java allows implicit widening conversions from integer types to floating-point types. While these conversions are safe in terms of range (the floating-point type can represent larger magnitudes), they can silently lose precision.

Floating-point types use binary representation with limited precision:

When you convert larger integer values to these types, the least significant digits may be rounded, resulting in a value that differs from the original integer. This happens silently because Java permits the conversion without requiring a cast.

For example, converting the int value 16777217 to float results in 16777216.0 - the original value cannot be represented exactly and gets rounded to the nearest representable value.

This is particularly problematic when:

Making these conversions explicit with a cast documents that you are aware of potential precision loss and helps prevent accidental data corruption.

What is the potential impact?

Data Corruption

Precision loss in conversions can lead to incorrect values being stored or processed. When working with identifiers, database keys, or financial calculations, even small rounding errors can have serious consequences.

Logic Errors

Code that compares converted values for equality may produce unexpected results. A value converted to float and back may not equal the original integer, breaking assumptions in your logic.

Debugging Difficulty

Silent precision loss is hard to detect and diagnose. The code compiles without warnings, and the loss may only manifest under specific input values, making bugs intermittent and difficult to reproduce.

How to fix it

The right fix depends on whether the precision loss is acceptable:

Simply adding a cast may hide a bug, which is why no automatic fix is proposed.

For integer arithmetic expressions such as a / b converted to a floating-point type, casting the result would not fix the integer division or overflow. Instead, one of the operands should be cast before the operation, as described by rule {rule:java:S2184}. Such expressions are not reported by this rule when {rule:java:S2184} already reports them.

Code examples

Noncompliant code example

public class DataProcessor {
    public void processId(int id) {
        float floatId = id; // Noncompliant - implicit conversion may lose precision
        System.out.println(floatId);
    }

    public void processTimestamp(long timestamp) {
        double value = timestamp; // Noncompliant - implicit conversion may lose precision
        System.out.println(value);
    }
}

Compliant solution

public class DataProcessor {
    public void processId(int id) {
        double doubleId = id; // A double can represent every int exactly
        System.out.println(doubleId);
    }

    public void processTimestamp(long timestamp) {
        double value = (double) timestamp; // Explicit cast documents that the precision loss is acceptable
        System.out.println(value);
    }
}

Resources

Documentation