数字提升会使用缩小转换吗?

huangapple go评论94阅读模式
英文:

Does numeric promotion use narrowing conversion?

问题

我一直以为数值转换使用扩展转换,就像是遵循一些优先级一样简单,比如:

如果操作数的类型为double,则将所有内容转换为double。下一个优先级是float,然后是long,然后是int。

但是后来我偶然发现了这个新的JLS,它说:

> 如果任何表达式的类型为int且不是常量表达式(§15.29),则升级类型为int,而不是int类型的其他表达式将进行扩展原始转换为int。
>
> 否则,如果任何表达式的类型为short,且每个其他表达式要么是short类型,要么是byte类型,或者是int类型的可在short类型中表示的具有值的常量表达式,则升级类型为short,并且byte表达式将进行扩展原始转换为short,int表达式将进行缩小原始转换为short。
>
> 否则,如果任何表达式的类型为byte,且每个其他表达式要么是byte类型,要么是可在byte类型中表示的int类型的常量表达式,则升级类型为byte,int表达式将进行缩小原始转换为byte。
>
> 否则,如果任何表达式的类型为char,且每个其他表达式要么是char类型,要么是可在char类型中表示的int类型的常量表达式,则升级类型为char,int表达式将进行缩小原始转换为char。
>
> 否则,升级类型为int,所有不是int类型的表达式将进行扩展原始转换为int。

你能告诉我这是什么意思吗?何时会使用缩小转换进行数值升级?如果这是一个愚蠢的问题,我很抱歉,因为我不是英语为母语的人,这对我而言变得过于英语化了,难以理解。

英文:

I always thought that numeric conversion uses widening conversion and is as simple as just following some priorities like:

If the operands are of type double, everything is converted to double. Next priority being float, then long and then int.

But then I stumbled upon this new JLS which says

> If any expression is of type int and is not a constant expression
> (§15.29), then the promoted type is int, and other expressions that
> are not of type int undergo widening primitive conversion to int.
>
> Otherwise, if any expression is of type short, and every other
> expression is either of type short or of type byte or a constant
> expression of type int with a value that is representable in the type
> short, then the promoted type is short, and the byte expressions
> undergo widening primitive conversion to short, and the int
> expressions undergo narrowing primitive conversion to short.
>
> Otherwise, if any expression is of type byte, and every other
> expression is either of type byte or a constant expression of type int
> with a value that is representable in the type byte, then the promoted
> type is byte, and the int expressions undergo narrowing primitive
> conversion to byte.
>
> Otherwise, if any expression is of type char, and every other
> expression is either of type char or a constant expression of type int
> with a value that is representable in the type char, then the promoted
> type is char, and the int expressions undergo narrowing primitive
> conversion to char.
>
> Otherwise, the promoted type is int, and all the expressions that are
> not of type int undergo widening primitive conversion to int.

Can you tell me what does this mean? When does numeric promotion use narrowing conversion? I am sorry if this is a dope question but I'm a non-English speaker and this is getting too english for me to understand.

答案1

得分: 1

如果两个值具有不同的数据类型,Java 将自动将其中一个值提升为较大的数据类型。

如果其中一个值是整数,另一个值是浮点数,Java 将自动将整数值提升为浮点数值的数据类型。

较小的数据类型,即 byte、short 和 char,在与 Java 二元算术运算符一起使用时,将首先被提升为 int,即使操作数中没有一个是 int。

在所有提升已发生并且操作数具有相同数据类型之后,结果值的数据类型将与其提升的操作数相同。

缩小转换仅影响根据您发布的内容,即那些既是常量又可在较窄类型中表示的 int 类型的数值表达式。

英文:

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

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.

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

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.

Narrowing conversion only affects, as per what you posted, numeric expressions of type int that are both a constant and representable in the narrower type.

huangapple
  • 本文由 发表于 2020年8月17日 15:04:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/63446084.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定