• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Bear Bibeault
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • salvin francis
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Frits Walraven
Bartenders:
  • Jj Roberts
  • Carey Brown
  • Scott Selikoff

Postfix operator

 
Ranch Hand
Posts: 268
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
Why does thecode prints 3,3 and not 3,4 ?
Firstly i is assigned 3 but where does the postfix goes...After i being assigned 3 shouldn't it be incremented by 1 ?

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Lajveet:

This expression
i*=2*i + i++
is evaluated as i= i*((2*i)+(i++))

Finally value which is assigned to i=3 not 4

Vydhehi
 
lowercase baba
Posts: 12965
66
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
we need to evaluate "i*=2*i + i++"

this basically gets converted to

i = i * (2*i + i++);

remember that the ++ gets done after everything else on the right-hand side, but BEFORE the assignment.

so basically we evaluate the rhs, which gives 3. then, i is incremented from 1 to 2. THEN we do the assignment, which assigns 3 to i.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
System.out.println(i*=2*i + i++);
i*=2*i+i++
i=i*(2*i)+i++(i=1)
i=i*2+i++(i=1)
i=2+i++(i=1)
i=2+1(i=2)
i=3-->print
System.out.println(i);
i=3-->print
 
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i *= 2*i + i++;

Jave evaluates expressions from left to right while respecting operator precedence. The first expression encountered is the compound assignment operator, *=. Since E1 op= E2 is the same as E1 = (T)((E1) op (E2)) we can expand the original expression as follows.

i = (int)((i) * (2*i + i++));

Now the first operator encountered on the left is the simple assignment operator, =. The left operand is the variable i and the right operand is the entire expression ((i) + (2*i + i++)), because the simple assignment operator has lower precedence that all of the operators that follow it on the right.

Java will first evaluate the left hand operand of the first multiplication operator, so we can write the value 1 in place of i.

i = (int)((1) * (2*i + i++));

Java will see that the right hand operand of the first multiplication operator is the expression, (2*i + i++), so Java will evaluate the entire right hand operand. The evaluation of the right hand operand begins by evaluating the expresion 2*i. The result is as follows.

(2 + i++)

The next operand is the addition operator. The left operand is the value 2 and the right operand is the postfix expression i++. Both the left and the right operands are evaluated before the addition operation is complete.

(2 + 1)

At this point, the value of i is 2; because i was incremented by the postfix expression.

The result of the above expression is 3, so our original statement can be simplified as follows.

i = (int)((1) * (3)); // At this point, i = 2.

The above can be simplified as follows.

i = (int)(3);

Prior to the evaluation of the simple assignment expression, the value of i is 2. After the evaluation of the assignment expression the value of i will be 3 and the old value will be lost.

Please note that the above process demonstrates what Java does, but it is not the simplest way for people to evaluate expressions. For us, it is a little easier to first go through the entire expression and evaluate the postfix and prefix expression first. On a second pass through the expression from left to right we can then work through the other operators. I didn't demonstrate that approach above, because I wanted to demonstrate when the postfix expression really does increment the value of i.

Please also note that the real exam does not focus on operator precedence. The real exam assumes that programmers use parenthesis to control the order of evaluation.
 
Author
Posts: 201
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dan is right..

But you can use the shortcut for time...

i *=2 * i + i++;

i = i * 2 * i + i++;

i = 1 * 2 * 1 + 1(2);

i = 3(2)

i = 3 in print and 3 in statement after.
 
reply
    Bookmark Topic Watch Topic
  • New Topic