2

Possible Duplicate:
post increment operator java
What is x after “x = x++”?

Can somebody explain me the result of each one of the following small programs? :

public static void main(String[] args) {
    int a = 10;
    a = a++;
    System.out.println(a);
}

The result is: 10

Why not 11 since a should have been increased after the assignment? Is that the fact that it comes to different variables left and right of the opeartor = ?

The next one:

public static void main(String[] args) {
    int a = 10;
    a = ++a;
    System.out.println(a);
}

The result is: 11

Comprehensible, but the compiler presents the warning: "The assignment to variable a has no effect". The result dissents though.

Update:

I do not modify my original question but I add this comment to clarify that now I catch the meaning of the warning. That is, even without the assignment (by a plain statement ++a) the result would be the same (11).

Community
  • 1
  • 1
arjacsoh
  • 8,932
  • 28
  • 106
  • 166

7 Answers7

11

The value of a++ is a. ++ has higher precedence than =. So:

  1. The value of a is taken.
  2. a is incremented.
  3. The value as at (1) is stored into a.

So the value of a doesn't change.

You can figure out yourself what happens in the second case.

user207421
  • 305,947
  • 44
  • 307
  • 483
1

In first case assignment happens first and then increment. So you get value which was before increment. But in second case it gets incremented first and then gets assigned.

rai.skumar
  • 10,309
  • 6
  • 39
  • 55
1

Lets analyze the Byte code produced in each way -

int a = 10;
a = a++;
System.out.println(a); //Output - 10 

produced ByteCode -

0 bipush
2 istore_1
3 iload_1 
4 iinc      
7 istore_1
8 getstatic
11 iload_1
12 invokevirtual
15 return

and

int a = 10;
a = ++a;
System.out.println(a); //Output -11

Here compiler give warning - The assignment to variable a has no effect

Produced ByteCode -

0 bipush
2 istore_1
3 iinc
6 iload_1
7 istore_1
8 getstatic
11 iload_1
12 invokevirtual
15 return

Here we can see in 1st case variable load first then increment so it does not effect anything to variable a.

Where as 2nd case it first increment then load the variable so it got effect of increment.

Subhrajyoti Majumder
  • 40,646
  • 13
  • 77
  • 103
  • +1 Good idea to look at what the opcode looks like, being the ultimate truth for all of us :). However, "Where as 2nd case it first increment then load the variable so it got effect of increment." suggests that the warning OP describes makes no sense, while it does. – TheBlastOne Dec 05 '12 at 14:11
  • Basically I just don't want 2 write against compiler so escape that. ;) – Subhrajyoti Majumder Dec 05 '12 at 16:33
0

This happens because a++ is more like a Method then a realistic Number.

you can splitt them into two seperat lines.

the first one would be

a = a;
return a + 1;

the seconde one would be

a = a+1;
SomeJavaGuy
  • 7,307
  • 2
  • 21
  • 33
0

a++ means increase the value of variable a, however a++ has the original value of a!

a = ?? means set the value of a to ??

So what you are doing is incrementing the value of a by one, and then setting it to the old value.

++a means increase the value of variable a, however ++a has the new value of a!

So in the second example you are incrementing the value of a, and then setting the value of a to that value (it has just gotten), so you are doing the same twice.

Thirler
  • 20,239
  • 14
  • 63
  • 92
0

a++ is equivalent to a = a + 1 but increments after the value is used in expression (assignment here) ++a is same but increments before its used in expression. a = ++a; has no explicit assignment affect as it is equal to a = a = a + 1

vishal_aim
  • 7,636
  • 1
  • 20
  • 23
-2

When you write a++ it means a will increment AFTER the '=' operator has been processed. So

b = a++;
System.out.println(a);
System.out.println(b);

Should produce on your screen: 11, 10. Because a becomes 11 after b takes the value of 'a'. If you change the a++ to ++a you force the increment to happen before the '=' is processed so you will be giving b = a + 1 and on your screen you should be having 11,11.

LonWolf
  • 132
  • 4
  • 3
    ++ does not increment after the program has passed the current line but after the term the ++ operator operates on has been evaluated. So in more complex terms, like b=(a++)++, this makes a difference. – TheBlastOne Dec 05 '12 at 09:16
  • Wrong. It has nothing to do with the current line. – user207421 Dec 05 '12 at 09:17
  • it does have to do with this simple example => easier to understand. I know the presumption fails on multiple operators, but he just asked the difference. – LonWolf Dec 05 '12 at 09:20
  • @LonWolf I repeat: it doesn't have anything to do with lines. It has to do with the defined semantics of the Java language, *none* of which depends on lines. – user207421 Dec 05 '12 at 09:22
  • @TheBlastOne `(a++)++` isn't legal Java code. The operand of `++` is a variable, not a value. – user207421 Dec 05 '12 at 09:29
  • true. But you got the idea :-) how about a=(b=(a++))++ ; ;-D – TheBlastOne Dec 05 '12 at 09:38
  • Fixed the answer to the right one then. I know Java operators have nothing to do with lines and it wasn't the best answer if it was to be compared with the technical one, my intentions were merely to help understand even though it wasn't really right. – LonWolf Dec 05 '12 at 09:48
  • @TheBlastOne I don't have an answer to your meaningless question. Nobody does. It's meaningless. – user207421 Dec 05 '12 at 10:45