Java 为什么赋值的左侧不能是增量表达式?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2443537/
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-08-13 07:44:20  来源:igfitidea点击:

Why can't the left hand side of an assignment be an increment expression?

javaarrays

提问by Stardust

Could any one please tell me the meaning of "++" with array in the following code in Java:

谁能告诉我以下Java代码中带有数组的“++”的含义:

   int [ ] arr = new int[ 4 ];
   for(int i = 0; i < arr.length; i++){
        arr[ i ] = i + 1;
       System.out.println(arr[ i ]++);
   }

what is arr[ i ]++meaning in above code, and why we can't do like:

arr[ i ]++上面代码的含义是什么,为什么我们不能这样做:

arr[ i ]++ = i + 1;

采纳答案by polygenelubricants

The operator being discussed here is called the postfix increment operator (JLS 15.14.2). It is specified to behave as follows:

此处讨论的运算符称为后缀增量运算符 ( JLS 15.14.2)。它被指定为如下行为:

  1. At run time, if evaluation of the operand expression completes abruptly, then the postfix increment expression completes abruptly for the same reason and no incrementation occurs.
  2. Otherwise, the value 1 is added to the value of the variable and the sum is stored back into the variable.
    1. Before the addition, binary numeric promotion (§5.6.2) is performed on the value 1 and the value of the variable.
    2. If necessary, the sum is narrowed by a narrowing primitive conversion (§5.1.3) and/or subjected to boxing conversion (§5.1.7) to the type of the variable before it is stored.
  3. The value of the postfix increment expression is the valueof the variable before the new value is stored.
  1. 在运行时,如果操作数表达式的计算突然完成,则后缀增量表达式出于相同的原因突然完成并且不会发生增量。
  2. 否则,将值 1 添加到变量的值上,并将总和存储回变量中。
    1. 在加法之前,对值 1 和变量的值执行二进制数字提升(第 5.6.2 节)。
    2. 如有必要,在存储变量之前,总和通过缩小原语转换(第 5.1.3 节)和/或进行装箱转换(第 5.1.7 节)缩小到变量的类型。
  3. 后缀增量表达式的值是所述值的变量的存储在新的值之前

The last point is the key for this question: the reason why you can't do arr[i]++ = v;is the same exact reason why you can't do x++ = v;; the postfix increment expression returns a value, not a variable.

最后一点是这个问题的关键:你不能做的原因和你不能做arr[i]++ = v;的完全相同x++ = v;;后缀增量表达式返回一个,而不是一个变量

From JLS 15.1 Evaluation, Denotation and Result:

JLS 15.1 评估、外延和结果

When an expression in a program is evaluated (executed), the result denotes one of three things:

  • A variable [...] (in C, this would be called an lvalue)
  • A value [...]
  • Nothing (the expression is said to be void)

当程序中的表达式被评估(执行)时,结果表示以下三件事之一:

  • 一个变量 [...](在 C 中,这将被称为左值)
  • 一个值 [...]
  • 无(该表达式被称为无效)

An assignment needs a variableon the left hand side, and a value is NOT a variable, and that's why you can't do x++ = v;.

赋值需要一个变量在左侧和值不是一个变量,这就是为什么你不能这样做x++ = v;

From JLS 15.26 Assignment Operators:

来自JLS 15.26 赋值运算符

The result of the first operand of an assignment operator must be a variable, or a compile-time error occurs. This operand may be a named variable [...], or it may be a computed variable, as can result from a field [...] or an array access. [...]

赋值运算符的第一个操作数的结果必须是变量,否则会发生编译时错误。该操作数可以是命名变量 [...],也可以是计算变量,如字段 [...] 或数组访问的结果。[...]

The following snippet shows erroneous attempts to assign to a value, going from rather subtle to more obvious:

下面的代码片段显示了对value赋值的错误尝试,从相当微妙到更明显:

int v = 42;
int x = 0;
x = v;        // OKAY!
x++ = v;      // illegal!
(x + 0) = v;  // illegal!
(x * 1) = v;  // illegal!
42 = v;       // illegal!
   // Error message: "The left-hand side of an assignment must be a variable"

Note that you canuse the postfix increment operator somewhereon the left hand side of an assignment operator, as long as the final result is a variable.

请注意,只要最终结果是变量,您就可以在赋值运算符左侧的某处使用后缀增量运算符。

int[] arr = new int[3];
int i = 0;
arr[i++] = 2;
arr[i++] = 3;
arr[i++] = 5;
System.out.println(Arrays.toString(arr)); // prints "[2, 3, 5]"

回答by sepp2k

arr[i]++means "increase the value of arr[i]by 1" and return the old value. So your code first sets arr[i]to i+1using arr[ i ] = i + 1;. It then increases it to i + 2using arr[ i ]++and prints the value it had beforeit was increased the second time, i.e. i + 1.

arr[i]++表示“将 的值增加arr[i]1”并返回旧值。所以你的代码首先设置arr[i]i+1使用arr[ i ] = i + 1;. 然后将它增加到i + 2usingarr[ i ]++并打印它在第二次增加之前的值,即i + 1.

You can't use arr[ i ] = arr[i] + 1;instead because it means "increase the value of arr[i]by 1 and return the new value".

您不能arr[ i ] = arr[i] + 1;改用,因为它的意思是“将 的值增加arr[i]1 并返回新值”。

You can't do arr[ i ]++ = i + 1;because it doesn't make sense.

你不能这样做,arr[ i ]++ = i + 1;因为它没有意义。

回答by Micha? Niklas

arr[ i ]++increases arr[i] by 1. It could be like:

arr[ i ]++将 arr[i] 增加 1。它可能是这样的:

arr[i] = i + 1;

As for arr[ i ]++ = i + 1;please do not try to write such code. Even if it compiles it will be puzzle for you or for others.

至于arr[ i ]++ = i + 1;请不要尝试编写这样的代码。即使它可以编译,对您或其他人来说也是一个难题。

PS I would prefer:

PS我更喜欢:

++arr[i];

回答by zellio

arr[i]++ is increase the value of arr[i] by one and assign

arr[i]++ 是将 arr[i] 的值加一并赋值

as for arr[ i ]++ = i + 1;This phrase means something entirely different, I don't know that it is even valid java to be honest. I would, if it works, incriment the value at arr[i] and then asign it to the index + 1;

至于arr[ i ]++ = i + 1;这句话的意思完全不同,老实说,我不知道它甚至是有效的java。如果可行,我会在 arr[i] 处增加值,然后将其分配给索引 + 1;

回答by Mark Byers

The code System.out.println(arr[i]++)means this:

代码的System.out.println(arr[i]++)意思是:

int tmp = arr[i];
arr[i] = arr[i] + 1;
System.out.println(tmp);

Your second example doesn't make sense because you can't use the ++operator on a value.

您的第二个示例没有意义,因为您不能++在值上使用运算符。

回答by dan04

The ++ operator has nothing to do with arrays. It increments any integer variable (or more generally, any lvalue) by 1. It's the same as the i++ in the loop.

++ 运算符与数组无关。它将任何整数变量(或更一般地,任何左值)增加 1。它与循环中的 i++ 相同。

You can write either ++x or x++. These both increment x, but they have different values: ++x returns the new value of x and x++ returns the old value. Thus, your code prints 1, 2, 3, 4 instead of 2, 3, 4, 5.

你可以写 ++x 或 x++。它们都增加 x,但它们有不同的值:++x 返回 x 的新值,x++ 返回旧值。因此,您的代码打印 1, 2, 3, 4 而不是 2, 3, 4, 5。