简体   繁体   English

为什么字符串转换不是数字提升的类型?

[英]Why is String conversion not a type of Numeric Promotion?

Oracle specifies about Numeric Promotion that Oracle 指定关于数字促销

Numeric promotion (§5.6) brings the operands of a numeric operator to a common type so that an operation can be performed.数字提升(第 5.6 节)将数字运算符的操作数变为通用类型,以便可以执行操作。

If either operand is of type double, the other is converted to double.如果任一操作数的类型为 double,则另一个将转换为 double。

Otherwise, if either operand is of type float, the other is converted to float.否则,如果任一操作数的类型为 float,则另一个将转换为 float。

Otherwise, if either operand is of type long, the other is converted to long.否则,如果任一操作数的类型为 long,则另一个将转换为 long。

Otherwise, both operands are converted to type int.否则,两个操作数都被转换为 int 类型。

Oracle specifies about String conversion that Oracle 指定关于字符串转换

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.如果只有一个操作数表达式是字符串类型,则在运行时对另一个操作数执行字符串转换(第 5.1.11 节)以生成字符串。 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.字符串转换(第 5.4 节)仅适用于二进制 + 运算符的操作数,当另一个操作数是字符串时,该操作数不是字符串。

My question is why isn't String conversion included in the Numeric Promotion?我的问题是为什么数字促销中不包括字符串转换? If you consider this code System.out.println("HELLO"+90+90.0F);如果你考虑这段代码System.out.println("HELLO"+90+90.0F); the output will be "HELLO9090.0F" (Of course without the quotes).输出将是“HELLO9090.0F”(当然没有引号)。 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.由于“HELLO”是一个字符串,相应的操作数 90 也将被转换为字符串,因为 '+' 运算符存在。 Concatenation occurs, thus giving "HELLO90".发生连接,从而给出“HELLO90”。 Now the corresponding operand will also be converted to a String as '+' operator exists.现在相应的操作数也将转换为字符串,因为 '+' 运算符存在。 Concatenation takes place, giving "HELLO9090.0F".发生串联,给出“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.例如,将数字传递给需要String的方法并让该转换自动生效是不可取的,因为这肯定会导致错误增加,因为人们将错误的参数传递给他们的方法和他们的代码仍然编译。 On the other hand, passing an int to a method expecting a long is generally reasonable and users generally expect the results they get.另一方面,将int传递给期望long的方法通常是合理的,用户通常期望他们得到的结果。

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.所有这一切意味着有一个特定术语描述数字类型之间的转换,而另一个术语用于其他类型的转换,并且该特定术语用于其他类型的转换不合适的地方。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM