简体   繁体   English

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

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

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;

The operator being discussed here is called the postfix increment operator ( JLS 15.14.2 ). 这里讨论的运算符称为后缀增量运算符( JLS 15.14.2 )。 It is specified to behave as follows: 它被指定为表现如下:

  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添加到变量的值中,并将总和存储回变量中。
    1. Before the addition, binary numeric promotion (§5.6.2) is performed on the value 1 and the value of the variable. 在添加之前,对值1和变量的值执行二进制数字提升(第5.6.2节)。
    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. 如果必要,在存储之前,通过缩小的基元转换(第5.1.3节)和/或经过装箱转换(第5.1.7节)将总和缩小到变量的类型。
  3. The value of the postfix increment expression is the value of the variable before the new value is stored . 后缀增量表达式的值是变量的值被存储在新的值之前

The last point is the key for this question: the reason why you can't do arr[i]++ = v; 最后一点是这个问题的关键:你不能做的原因arr[i]++ = v; is the same exact reason why you can't do x++ = v; 与你不能做x++ = v;原因完全相同x++ = v; ; ; the postfix increment expression returns a value , not a variable . 后缀增量表达式返回一个 ,而不是一个变量

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) 变量[...](在C中,这将被称为左值)
  • A value [...] 一个值 [...]
  • Nothing (the expression is said to be void) 没什么(表达被认为是无效的)

An assignment needs a variable on 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: 以下代码段显示了错误的分配值的尝试,从相当微妙到更明显:

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 can use the postfix increment operator somewhere on 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]"

arr[i]++ means "increase the value of arr[i] by 1" and return the old value. arr[i]++表示“将arr[i]的值增加1”并返回旧值。 So your code first sets arr[i] to i+1 using arr[ i ] = i + 1; 因此您的代码的第一组arr[i]i+1使用arr[ i ] = i + 1; . It then increases it to i + 2 using arr[ i ]++ and prints the value it had before it was increased the second time, ie i + 1 . 然后使用arr[ i ]++将其增加到i + 2并打印第二次增加之前的值,即i + 1

You can't use arr[ i ] = arr[i] + 1; 你不能使用arr[ i ] = arr[i] + 1; instead because it means "increase the value of arr[i] by 1 and return the new value". 相反,因为它意味着“将arr[i]的值增加1并返回新值”。

You can't do arr[ i ]++ = i + 1; 你不能做arr[ i ]++ = i + 1; because it doesn't make sense. 因为它没有意义。

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. 你的第二个例子没有意义,因为你不能在值上使用++运算符。

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++. 您可以编写++ x或x ++。 These both increment x, but they have different values: ++x returns the new value of x and x++ returns the old value. 这些都增加x,但它们具有不同的值:++ x返回x的新值,x ++返回旧值。 Thus, your code prints 1, 2, 3, 4 instead of 2, 3, 4, 5. 因此,您的代码打印1,2,3,4而不是2,3,4,5。

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; 至于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. 即使它编译它也会让你或其他人感到困惑。

PS I would prefer: PS我更喜欢:

++arr[i];

arr[i]++ is increase the value of arr[i] by one and assign arr [i] ++将arr [i]的值增加1并赋值

as for arr[ i ]++ = i + 1; 至于arr[ i ]++ = i + 1; This phrase means something entirely different, I don't know that it is even valid java to be honest. 这句话意味着完全不同的东西,我不知道它甚至是有效的Java诚实。 I would, if it works, incriment the value at arr[i] and then asign it to the index + 1; 如果它有效,我会在arr [i]中输入值,然后将其指向索引+ 1;

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

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