4

Possible Duplicate:
Is there a difference between x++ and ++x in java?

Can anyone please explain me what is happening backyard to these statements?

int x=5;
 System.out.println((x++)*x); //Gives output as 30




int x=5;
 System.out.println((++x)*x); //Gives output as 36.
Community
  • 1
  • 1
user1500024
  • 213
  • 1
  • 3
  • 9

5 Answers5

7
int x=5;
 System.out.println((x++)*x); //Gives output as 30

You first take x (x = 5) as an operand. Then it's incremented to 6 which is second operand.

int x=5;
 System.out.println((++x)*x); //Gives output as 36.

You first increment x by one (x = 6) and then multiply by x => 6 * 6 = 36

ioreskovic
  • 5,531
  • 5
  • 39
  • 70
  • 2
    The underlying reason is that multiplicative operations are evaluated from left to right. If that were not the case, the result would be different. – assylias Jul 19 '12 at 09:38
4
p++ means use then increment with a copy (copy is extremely local)

++p means increment and use without a copy


p++: use 5 and use incremented thing later in that line

++p: increment and use immediately
huseyin tugrul buyukisik
  • 11,469
  • 4
  • 45
  • 97
4

Multiplication is left-to-right associative, so the left operand will be evaluated first, then the right operand.

Post-increment operator will evaluate to current value of the variable, and increment it right after.

Pre-increment operator will increment the variable, then evaluate to the incremented value.

    (x++) * x (x = 5)
--> 5 * x (increment deferred, x = 5)
--> 5 * x (increment x, x = 6)
--> 5 * 6
--> 30

    (++x) * x (x = 5)
--> 6 * x (x is incremented before evaluated into expression, x = 6)
--> 6 * 6
--> 36

I mentioned the associativity here because it will affect the final result. If the associativity of multiplication is right-to-left instead of left-to-right, then the result will be 25 and 30 for post-increment and pre-increment expression respectively.

nhahtdh
  • 55,989
  • 15
  • 126
  • 162
4

Assuming you understand that:

  • ++x returns x+1 and increments x, while
  • x++ returns x and increments x,

the reason for the result is defined by the Java Language Specification #15.17

The multiplicative operators have the same precedence and are syntactically left-associative (they group left-to-right).

So in the first case, x++ is first evaluated, which returns 5 (it is the postfix operator) and adds 1 to x afterwards. Then the result of x++ (5) is multiplied by x (which is now 6) ==> 30

In the second case, ++x is first evaluated, which adds 1 to x and returns 6 (it is the prefix operator). Then the result of ++x (6) is multiplied by x (which is now 6) ==> 36

assylias
  • 321,522
  • 82
  • 660
  • 783
3

Post increment operators do the incrementation after the expression has been calculated.

In your first example, what is actually happening is:-

(x++)  * x ; // (now incremented, 6 )
// x (5) * x+1 (6)

In your second example, the increment happens first

(++x)  * x; // incremented straight away.
// x+1 (6) * x (now 6)
Paul Alan Taylor
  • 10,474
  • 1
  • 26
  • 42