• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

post increment operator

 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi to all great peoples there,
Can anyone explain what's happening here....
public class A{
public static void main(String[] args){
int i = 12;//line1

i = i++;//line2

i = i++;//line3

i = i++;//line4

System.out.println(i);
}
}
The result stays with the initial of 12...But why?
 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,
In this program, everytime i gets assigned before its value gets changed
by the post-increment operator. So 12 gets assigned everytime.
To understand better, try assigning i++ to a different variable.
say
int i = 12;
j =i++;
Here you will end up with i = 13 and j =12. i is assigned to j before it's incremented.
correct me if I am wrong.
Kalai Ganesh.
 
Ranch Hand
Posts: 522
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Vicky, accprding to JLS , the post increment operator always return its old value then increment the number, although the '=' has a higher priority than '++'.
check this code

the result is 22. (x = 12 + 10)
Hope this helps.
 
Kalai Ganesh
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hai Vicken,
In this code:
--------------------------------------------------------------------------------
class test { public static void main (String[] args) {
int x=10;
x = ++x + x++;
System.out.println(x); }}
----------------------------------------------------------------------------
I tried to understand it with different examples of how the ++x result in 12 here. But in vain.
Can you please explain what's happening?
Thanks.
 
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's as if the vm is evaluating the righthand side of the assignment statement, building up its results in a temporary variable. Let's call that variable temp. For i = i++ the steps are:
  • temp = i
  • increment i
  • i = temp
  • It might be easier to see if the assignment were just slightly more complicated, say, i = i++ + 1. Then:
  • temp = i + 1
  • increment i
  • i = temp
  • In this case the result is always i + 1.
    [ September 20, 2003: Message edited by: Steve Lovelace ]
     
    Vicken Karaoghlanian
    Ranch Hand
    Posts: 522
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    hi Kalai, it is pretty simple, just follow the rule: the post increment operator always return its old value then increment the number

    the value of x is 10
    1) Right Hand Side of the equation is evaluated first (x++), now according to that previous rule 'x' is incremented by one (becoming 11) but returning its old value which is 10.
    2) Left Hand Side is evaluated next, by now the value of x is 11 (rememeber that... it is NOT 10 as you might expect), incrementing it will result 12.
    3) 12 + 10 = 22.
    Hope this help.
     
    Kalai Ganesh
    Ranch Hand
    Posts: 32
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    I got it wrong since I was doing the left operand first.
    Thanks!
     
    Greenhorn
    Posts: 16
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hi!
    I think you are wrong Vicken.
    First the operators preference is different that you have said. Please look at this link Java Operators, Precedence, and Associativity and you will see that the assigment operator is the lowest one in priority.
    Now i will try to explain what means i = i++ and i = ++i;
    a) i = i++;
    1. the right i is evaluated and its value is stored to be used in the operation (the assigment)
    2. the right i is incremented by one and the value is assigned to the right variable i
    3. finally the left variable i is assigned the value of the operation (calculated in the first step)
    b) i = ++i;
    1. the right i is incremented by one and the value is assigned to the right variable i
    2. the right i (that has been previously incremented) is evaluated and its value is stored to be used in the operation
    3. finally the left variable i is assigned the value of the operation (calculated in the second step)
    if you would like to be sure i can advice you to use the "javap" command
    Finally, we can try this program again

    1. the = operator is always the last operator evaluated so we start evaluating ++x + x++ and we start evaluating from left to right!
    2. ++x increments the value of x that now is 11 and this value will be used in the + expression
    3. x++ increments the value of x but before that the value of x will be stored to be used in the + expression (so the value of x before incremented --> 11 will be used in the + expression and after the storage the value of x is incremented by one... now x = 12)
    4. the + expression is evaluated--> 11 + 11 = 22 (not 12 + 10 = 22!!!)
    Here you can see the bytecode:
    Compiled from test.java
    class tests.test extends java.lang.Object {
    tests.test();
    public static void main(java.lang.String[]);
    }
    Method tests.test()
    0 aload_0
    1 invokespecial #1 <Method java.lang.Object()>
    4 return
    Method void main(java.lang.String[])
    0 bipush 10 -> puts 10 in the stack
    2 istore_1 -> stores the stack value (10) in the local variable 1
    3 iinc 1 1 -> increments the local variable 1 by 1 (++x) --> x=11
    6 iload_1 -> puts the local variable 1 in the stack (value=11)
    7 iload_1 -> puts the local variable 1 in the stack (value=11) before it is incremented
    8 iinc 1 1 -> increments the local variable 1 by 1 (x++) --> x=12
    11 iadd -> the + operator is evaluated getting the two values from the stack and the result is putted in the stack (11 + 11 = 22)
    12 istore_1 -> stores the stack value (22) in the local variable 1 (now x = 22)
    13 getstatic #2 <Field java.io.PrintStream out>
    16 iload_1
    17 invokevirtual #3 <Method void println(int)>
    20 return
    And here you have got the link i use to understand the bytecode commands JVM reference
    Hope it helps!
     
    Greenhorn
    Posts: 15
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Armando, glad to know that you figure it out since vicken's answer bothers me a lot. Just to make is simplofies:
    int i=10;
    int j = ++i + i++;
    step 1: ++i, i=11
    step 2: j = i+i = 11+11 =22
    step 3: i++, i = 12
    BTW, will you please tell me what is "javap"?
    Thank you.
     
    Vicken Karaoghlanian
    Ranch Hand
    Posts: 522
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    hi armando,
    originally posted by Vicken


    the '=' has a higher priority than '++'.


    sorry about this statemet it is wrong, i don't know what i was thinking when i wrote it. yes armando you are correct prefix and postfix operator has higher priority that '+'. However my statement x = 12 + 10 is correct.
    Now let me prove that you are wrong.
    this example is from the 'K&B' Book:


    class MathTest {
    public static void main (String [] args) {
    int x = 10;
    x = ++x + x++;
    System.out.println(x);
    }
    }
    You might think the output will be 21, but let�s break this down. The postfix ++ has precedence, so the right side will increment x to 11, but return 10. The left side will then increment x from 11 to 12. So the equation actually looks like x = 12 + 10, and the result is 22. The exam will likely include a few simple precedence questions like this.



    now let us examine this code...
    a) in this equation the postfix operator has the higest priority (x++), hence it will be evaluated first.
    b) the next operator to be evaluated is the prefix (++x) operator.
    c) now it is the turn of the unary operator (+) to be evaluated, which is done from right-to-left.
    the approach you made in evaluating the expression is correct, but the only mistake you made is that you evaluated the prefix operator first which is WRONG.
    The order of evaluation is:
    1) x++ (Postfix)
    2) ++x (Prefix)
    3) +
    4) =
    you can read my very first post, and see that x = 12 + 10 is correct.
    I hope this make some sense to you now.
    [ September 22, 2003: Message edited by: Vicken Karaoghlanian ]
     
    Ranch Hand
    Posts: 56
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    I wrote a few lines worth of example and by what I'm seeing I'd say Armando is right. (As I suspected)

    After I compiled that I read with javap what the bytecode said:

    In the main method this is what happens (line numbers refer to the line numbers given by javap)
    line 0: Store integer calue 10 on the stack
    line 2: Store the integer value in stack to local variable 1
    line 3: Increment local variable 1 by 1
    line 6: Push the local variable 1 to stack (the value is 11)
    line 7: Push the local variable 1 to stack (the value is 11)
    line 8: Increment local variable 1 by 1
    line 11: Add the two top operands in stack together and place the result on top of stack
    line 12: Store the stack's top value to local variable 1
    So I'd say the operations are evaluated from left to right, and operations on a single operand are evaluated according to precedence.
    That's my take on the matter.
    [ September 22, 2003: Message edited by: Mika Leino ]
    [ September 22, 2003: Message edited by: Mika Leino ]
     
    Vicken Karaoghlanian
    Ranch Hand
    Posts: 522
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    hmmm, good point Mika and Armando, i believe you were right and i was wrong.
    this code shows that the expression is evluated from left-to-right and NOT right-to-left as i thought.

    thanks guys for bringing this up.
     
    Armando Anton
    Greenhorn
    Posts: 16
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hi again!
    yes the operator is evaluated from right to left and i think that a good choice is to play with examples and theirs bytecodes to improve the ideas
    Javap is used to disassembles class files. Here you have got an URL
     
    I am not young enough to know everything. - Oscar Wilde This tiny ad thinks it knows more than Oscar:
    a bit of art, as a gift, that will fit in a stocking
    https://gardener-gift.com
    reply
      Bookmark Topic Watch Topic
    • New Topic