tags:

views:

182

answers:

7

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;
+2  A: 

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
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
@MarkByers: You're right, of course. Fixed.
sepp2k
A: 

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
but `++arr[i]` will print a different result as `arr[i]++`.
Carlos Heuberger
Yes, you are right. I would do increasing:`++arr[i]` in one line and printing in another. I think this will be more readable.
Michał Niklas
A: 

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;

Mimisbrunnr
+1  A: 

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
+1  A: 

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
A: 

No one's mentioned this yet:
the reason you can't do

arr[ i ]++ = i + 1;

is that you can't change a variable twice in one statement. The more common form of this is:

x = x++; //illegal!
BlueRaja - Danny Pflughoeft
Isn't the reason you can't do `arr[ i ]++ = i + 1;` rather that `arr[i]++` is not an lvalue? I mean it returns a temporary int - what would assigning to it even mean?
sepp2k
`x = x++;` is _perfectly legal_ in Java (see: http://stackoverflow.com/questions/2377145/unable-to-make-out-this-assignment-in-java/)
polygenelubricants
+2  A: 

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