Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The operator being discussed here is called the postfix increment operator (<a href="http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#15.14.2" rel="noreferrer">JLS 15.14.2</a>). It is specified to behave as follows:</p> <blockquote> <ol> <li>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.</li> <li>Otherwise, the value 1 is added to the value of the variable and the sum is stored back into the variable. <ol> <li>Before the addition, binary numeric promotion (§5.6.2) is performed on the value 1 and the value of the variable.</li> <li>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.</li> </ol></li> <li><em>The value of the postfix increment expression is <strong>the value</strong> of the variable before the new value is stored</em>.</li> </ol> </blockquote> <p>The last point is the key for this question: the reason why you can't do <code>arr[i]++ = v;</code> is the same exact reason why you can't do <code>x++ = v;</code>; the postfix increment expression returns a <em>value</em>, not a <em>variable</em>.</p> <p>From <a href="http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#15.1" rel="noreferrer">JLS 15.1 Evaluation, Denotation and Result</a>:</p> <blockquote> <p>When an expression in a program is evaluated (executed), the result denotes one of three things:</p> <ul> <li>A variable [...] (in C, this would be called an lvalue)</li> <li>A value [...]</li> <li>Nothing (the expression is said to be void) </li> </ul> </blockquote> <p>An assignment needs a <em>variable</em> on the left hand side, and a value is NOT a variable, and that's why you can't do <code>x++ = v;</code>.</p> <p>From <a href="http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#15.26" rel="noreferrer">JLS 15.26 Assignment Operators</a>:</p> <blockquote> <p><em>The result of the first operand of an assignment operator must be a variable, or a compile-time error occurs</em>. This operand may be a named variable [...], or it may be a computed variable, as can result from a field [...] or an array access. [...]</p> </blockquote> <p>The following snippet shows erroneous attempts to assign to a <em>value</em>, going from rather subtle to more obvious:</p> <pre><code>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" </code></pre> <p>Note that you <em>can</em> use the postfix increment operator <em>somewhere</em> on the left hand side of an assignment operator, as long as the final result is a variable.</p> <pre><code>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]" </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload