Java – comparing numeric primitives

When comparing primitive numeric values and the two operands are not of the same type, the smaller one is promoted to the higher type.

For example,

The value 6f is of type float; while 6 is of type int.

During the comparison, 6 is promoted to float because 6f is of type float. The type int is the smaller type; while the type float is the larger type.

Internally, Java compares 2 float values.

This rule is based on Java Numeric Promotion rules.

Another example,

Here, we have 6 of int type and another 6 of short type. The type int is the larger type now; while the short type is the smaller type.

Internally, the comparison will be between 2 int values.

So, is there a way to prove or trace this promotion programmatically? Nope, as far as I know.

Karl San Gabriel

Karl San Gabriel

Java and Enterprise Technologies Expert