5

Could any one please tell me the meaning of "++" with array in the following code in 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 ]++ = i + 1;
Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
Stardust
  • 1,115
  • 6
  • 22
  • 33
  • 1
    Never seen such coding style (with spaces inside square braces) in java code. – Roman Mar 14 '10 at 20:31
  • As a side note, the bytecode for the ++ operator depends on whether it's used on a local int variable or not, and on whether the result is used or not. The simplest case is of a bare increment of a local int variable, where a single increment instruction is generated. Your case of postincrementing an array member generates some of the most complex bytecode. (I can't comment on any JIT transformation.) – Neil Mar 14 '10 at 21:54

6 Answers6

7

The operator being discussed here is called the postfix increment operator (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. 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 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; is the same exact reason why you can't do x++ = v;; the postfix increment expression returns a value, not a variable.

From JLS 15.1 Evaluation, Denotation and Result:

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)

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;.

From JLS 15.26 Assignment Operators:

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]"
polygenelubricants
  • 376,812
  • 128
  • 561
  • 623
2

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+1 using 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, i.e. 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".

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

sepp2k
  • 363,768
  • 54
  • 674
  • 675
  • 1
    The code doesn't first increase the value of arr[i]. That would be `arr[i] = arr[i] + 1`. But it says `arr[i] = i + 1`. That is different. – Mark Byers Mar 14 '10 at 20:30
2

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

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.

Mark Byers
  • 811,555
  • 193
  • 1,581
  • 1,452
1

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.

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.

dan04
  • 87,747
  • 23
  • 163
  • 198
0

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

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.

PS I would prefer:

++arr[i];
Michał Niklas
  • 53,067
  • 18
  • 70
  • 114
0

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

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;

zellio
  • 31,308
  • 1
  • 42
  • 61