Numeric Promotion in Java
Java feature for automatic conversion
Definition
Numeric Promotion is a feature from Java for an automatic conversion of a value to a larger data type in order to perform a mathematical operation.
Numeric Promotion
in Java is all about
universal balance.
int i = 12;
double d = 3.3;
double result = i * d; // 12.0 * 3.3 (int i is promoted to double)This balance has still
a set of fixed rules.
Rule 1 - Make it Larger
If two values have different data types, Java will automatically promote one of the values to the larger of the two data types.
Rule 2 - Floating Clouds
If one of the values is integral and the other is floating-point, Java will automatically promote the integral value to the floating-point value`s data type.
Rule 3 - int as a golden rule
Smaller data types, namely byte, short and char are first promoted to int any time theyre used with a Java binary arithmetic operator, even if neither of the operands is int.
Rule 4 - Universal balance
After all promotion has occurred and the operands have the same data type, the resulting value will have the same data type as its promoted operands.