Oracle specifies about Numeric Promotion that
Numeric promotion (§5.6) brings the operands of a numeric operator to a common type so that an operation can be performed.
If either operand is of type double, the other is converted to double.
Otherwise, if either operand is of type float, the other is converted to float.
Otherwise, if either operand is of type long, the other is converted to long.
Otherwise, both operands are converted to type int.
Oracle specifies about String conversion that
If only one operand expression is of type String, then string conversion (§5.1.11) is performed on the other operand to produce a string at run time. String conversion (§5.4) applies only to an operand of the binary + operator which is not a String when the other operand is a String.
My question is why isn't String conversion included in the Numeric Promotion? If you consider this codeSystem.out.println("HELLO"+90+90.0F);
the output will be "HELLO9090.0F" (Of course without the quotes). I totally understand how this works. As "HELLO" is a String, the corresponding operand 90 will also be converted to a String as '+' operator exists. Concatenation occurs, thus giving "HELLO90". Now the corresponding operand will also be converted to a String as '+' operator exists. Concatenation takes place, giving "HELLO9090.0F". I mean why isn't the String conversion included in the first priority of Numeric Promotion? It makes sense if you think as all the operands following the main String will be converted to Strings, provided only addition operator exists. It should be provided in the first priority of numeric promotion itself.
In the context of string concatenation, you can see conversions from numeric types to strings as a form of promotion. However, there are many contexts in which only conversion between numeric types, not other kinds of conversion, are appropriate, and the term numeric promotion is used to categorize those sorts of conversion.
For example, it would be undesirable to be able to pass a number to a method that expects a String
and have that conversion automatically take effect, as this would definitely lead to increased bugs as people passed the wrong arguments to their methods and their code still compiled. On the other hand, passing an int
to a method expecting a long
is generally reasonable and users generally expect the results they get.
All this means is that there is a specific term that describes conversion between numeric types, and a different term for other kinds of conversions, and that specific term is used in places where other kinds of conversion aren't appropriate.