Software Development

Java – comparing numeric primitives

[wp_ad_camp_1]

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.

[wp_ad_camp_2]

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.

[wp_ad_camp_3]

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

Loading

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