在 Java 中推广?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1660856/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 17:29:07  来源:igfitidea点击:

Promotion in Java?

java

提问by i2ijeya

The rules for promotion is "when operands are of different types, automatic binary numeric promotion occurs with the smaller operand type being converted to the larger". But the operands are of same type for example,

提升的规则是“当操作数的类型不同时,自动二进制数字提升发生,较小的操作数类型转换为较大的操作数类型”。但是操作数是相同的类型,例如,

byte=byte+byte // Compile time error... found int..

So why is it so?

那么为什么会这样呢?

回答by Jon Skeet

There's no + operator for byte. Instead, both operands are promoted to int, so you've got

没有 + 运算符byte。相反,两个操作数都被提升为 int,所以你有

byte = byte + byte
... becomes (widening to find + operator) ...
byte = int + int
... becomes (result of + operator) ...
byte = int 

... which then fails because there's no implicit conversion from intto byte. You need to cast:

...然后失败,因为没有从intto的隐式转换byte。你需要投:

byte a = 1;
byte b = 2;

byte c = (byte) (a + b);

Here are the actualrules for numeric promotion, from section 5.6.2 of the JLS:

以下是数字促销的实际规则,来自JLS 的第 5.6.2 节



When an operator applies binary numeric promotion to a pair of operands, each of which must denote a value that is convertible to a numeric type, the following rules apply, in order, using widening conversion (§5.1.2) to convert operands as necessary:

当运算符将二进制数字提升应用于一对操作数时,每个操作数都必须表示一个可转换为数字类型的值,以下规则按顺序应用,根据需要使用加宽转换(第 5.1.2 节)来转换操作数:

  • If any of the operands is of a reference type, unboxing conversion (§5.1.8) is performed. Then:
  • 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.
  • 如果任何操作数是引用类型,则执行拆箱转换(第 5.1.8 节)。然后:
  • 如果任一操作数的类型为 double,则另一个将转换为 double。
  • 否则,如果任一操作数的类型为 float,则另一个将转换为 float。
  • 否则,如果任一操作数的类型为 long,则另一个将转换为 long。
  • 否则,两个操作数都被转换为 int 类型。

回答by denis.zhdanov

You were provided with correct answer about automatic promotion to 'int'.

您已获得有关自动升级为“int”的正确答案。

There is one more note about that - compound assignment operators behave as they have an implicit type case. Example:

关于这一点还有一点要注意 - 复合赋值运算符的行为就像它们具有隐式类型情况一样。例子:

byte b1 = 1;
byte b2 = 2;
b1 = b1 + b2; // compilation fails
b1 += b2; // compilation successful

回答by Basheer AL-MOMANI

I would like to talk about Promotion in general

我想谈谈一般的促销

Java can evaluate only arithmetic expressions in which the operands' types are identical

Java 只能计算操作数类型相同的算术表达式

For example, in an expression containing int and double values, the int values are promoted to double values for use in the expression.

例如,在包含 int 和 double 值的表达式中,int 值被提升为 double 值以在表达式中使用。

in another word

换句话说

double someVar = 1 / 2;// someVar = 0

but

double someVar = (double)1 / 2;// someVar = 0.5

why?

为什么?

  • we use the (double) cast operatorto create a temporaryfloating-point copy of its operand "1"(it's called explicit conversion)
  • The calculation now consists of a floating-point value (the temporary double copy of 1) divided by the integer 2
  • according to the above statement, Java performs an operation called promotion(or implicit conversion), so the int valuesare promoted to double valuesfor use inthe expression => the integer 2 is promoted to double
  • the expression became double someVar = 1.0 / 2.0; // someVar= 0.5
  • 我们使用 (double)cast operator创建temporary其操作数的浮点副本"1"(称为explicit conversion
  • 计算现在包含一个浮点值(1 的临时双副本)除以整数 2
  • 根据上面的说法,Java执行了一个叫做promotion(或隐式转换)的操作,所以int values提升double values为使用in表达式=>整数2提升为double
  • 表情变成了 double someVar = 1.0 / 2.0; // someVar= 0.5

hope this is helpful, even if it is out of the core of the question

希望这是有帮助的,即使它超出了问题的核心

回答by Navdeep Singh

To understand this you should refer the 2 things:

要理解这一点,您应该参考两件事:

Implicit Casting: byte (8 bit) -> short (16 bit) -> int (32 bit) -> float (32 bit) -> double (64 bit) char (16 bit) -> int (32 bit)

隐式转换:byte(8 位)-> short(16 位)-> int(32 位)-> float(32 位)-> double(64 位)char(16 位)-> int(32 位)

Arithmetic Operator Rules

算术运算符规则

  1. Operator Precedence Rule : This rule states that Group of (*,/, %) will be evaluated first. Then Group of (+,-) operator will be evaluated. From a same Group of Operators, calculate from the left.

  2. Operand Promotion Rule : This rule states that Operands having data type smaller than int will be promoted to int. order of promotion (byte->short->int, char->int)

  3. Same Type Operand Rule: This rule states that if both operands are int,long,float,double then the same type is carried to the result type. i.e. long+long => long , float + float => float, int+int => int

  4. Mix Type Operand Rule : Follow the order of Promotion (int->long->float->double) if any of the operand is from the above order then the smaller will be promoted to the bigger one and result will be calculated in bigger type. i.e. long + double => double , int + long => long

  1. 运算符优先规则:此规则规定将首先评估 (*,/, %) 组。然后将评估 (+,-) 运算符组。从同一组运算符中,从左侧计算。

  2. 操作数提升规则:该规则规定数据类型小于 int 的操作数将被提升为 int。提升顺序(byte->short->int,char->int)

  3. 相同类型操作数规则:该规则规定,如果两个操作数都是 int,long,float,double 则相同的类型被携带到结果类型。即 long+long => long , float + float => float, int+int => int

  4. Mix Type Operand Rule : 遵循Promotion的顺序(int->long->float->double) 如果任何一个操作数来自上述顺序,那么较小的将提升为较大的,结果将以较大的类型计算. 即 long + double => double , int + long => long

回答by Not a bug

From this SO question, and above answers due to +arithmetic operator, both operands are converted to type int.

这个 SO question以及由于+算术运算符的上述答案,两个操作数都转换为 type int

byte b1 = 1;
byte b2 = 2;
byte b3 = b1 + b2; // compile time error

In above code, value of b1and b2will be resolved at runtime, so compiler will convert both to intbefore resolving the value.

在上面的代码中,价值b1b2在运行时得到解决,所以编译器都将转化为int解决前值。

But if we consider the following code,

但是如果我们考虑下面的代码,

final byte b1 = 1;
final byte b2 = 2;
int b3 = b1 + b2; // constant expression, value resolved at compile time

b1and b2are final variables and values will be resolved at compile time so compilation won't fail.

b1b2是最终变量,值将在编译时解析,因此编译不会失败。