Software Development

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.

Got comments or suggestions? We disabled the comments on this site to fight off spammers, but you can still contact us via our Facebook page!.

You Might Also Like