[ACCEPTED]-What is numeric promotion?-java

Accepted answer
Score: 23

Numeric promotion is the conversion of a 8 smaller numeric type to a larger numeric 7 type, so that integer and floating-point 6 operations may take place. In numerical 5 promotion, byte, char, and short values 4 are converted to int values. The int values 3 are also converted to long values, if necessary. The 2 long and float values are converted to double 1 values, as required.

Score: 5

Numeric Promotion Rules

  1. If two values have different data types, Java 13 will automatically promote one of the values 12 to the larger of the two data types.

  2. If one 11 of the values is integral and the other 10 is floating-point, Java will automatically 9 promote the integral value to the floating-point 8 value’s data type.

  3. Smaller data types, namely 7 byte, short, and char, are first promoted 6 to int any time they’re used with a Java 5 binary arithmetic operator, even if neither 4 of the operands is int.

  4. After all promotion 3 has occurred and the operands have the same 2 data type, the resulting value will have 1 the same data type as its promoted operands

Score: 1

Numeric promotion is a conversion of an 10 operand (at least one of the numbers involved) to 9 a common type.

For example:

int i = 10;
double d1 = 2.5;
double d2 = d1 * i;

In this case, i 8 is promoted to double so the calculation 7 can be performed. In some ways, you can 6 think of this is analogous to boxing, but 5 boxing involves moving from a struct to 4 an object (from the stack to the heap). But, using 3 the analogy does give an idea of the fact 2 the integral value is being made into a 1 floating point to perform the calculation.

Score: 0

If you look here, you will see the following quote:

Numeric 8 promotion (§5.6) brings the operands of 7 a numeric operator to a common type so 6 that an operation can be performed.

They 5 are referencing this section, where they give a variety of examples. The 4 classic example is that of an int times 3 a float. The integer is promoted to a float 2 so that the multiplied result is, therefore, a 1 float.

More Related questions