Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Assigning and Post incrementing:

 
Narasimha Rao B.
Ranch Hand
Posts: 205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Please clarify me, whether the below understanding of mine is correct or not?
int k = 3;
for(int i=0;i<5;i++){
System.out.println(k=k++)}
Above print statement will always print 3 and k value will not be incremented.
Reason is,
Initially k value is 3 and will be assigned to k, then k value will be incremented temporarily by one, i.e., now temporary k value is 4 and now I am not assigning the temporary value of k to any thing, hence it will lost and now the actual k value is 3, which I assigned before incrementing. Is this understanding is correct or not? If it is wrong what is actual logic behind this?
Thanks in Advance,
Narasimha.
 
Vicken Karaoghlanian
Ranch Hand
Posts: 522
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Forget the loop and let us focus on
int k = 3;
k = k++;
The JLS states that "The post increment operator returns its old value then increment the result"
Knowing this fact you can easily understand what happens:
  • 'k' is initialized to the value of 3
  • 'k++' is executed, thus k becomes 4 but it returns its old value which is 3
  • 3 is assigned to 'k'


  • Remember that the increment operator has a higher priority over the assignment; therefore it is executed first (not the other way around as you suggested).
    [ February 07, 2004: Message edited by: Vicken Karaoghlanian ]
     
    Davy Kelly
    Ranch Hand
    Posts: 384
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    hey narasimha,
    I like to look at it in a similar fashion but with a slightly different method.
    int k = 3;
    k = k++; //k is assigned to 3 here
    //but would be 4 here. because you have exited the loop, it does not get to be 4!
    so every time you see k = k++;in your example it is saying, k = 3
    Davy
     
    Vicken Karaoghlanian
    Ranch Hand
    Posts: 522
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Originally posted by Davy Kelly:
    hey narasimha,
    I like to look at it in a similar fashion but with a slightly different method.
    int k = 3;
    k = k++; //k is assigned to 3 here
    //but would be 4 here. because you have exited the loop, it does not get to be 4!
    so every time you see k = k++;in your example it is saying, k = 3
    Davy


    Davy, always remember the fact that the increment operator has a higher priority over the assignment; therefore it is executed first.
    How do you think the following expression is evaluated, and what is it output?
    int k = 0;
    k = k++ + ++k + k++;
    Don't ask the compiler for the answer, try doing it yourself.

    And by the way...

    I no my spelling is carp!!!

    I speaking english very best too.
    [ February 07, 2004: Message edited by: Vicken Karaoghlanian ]
     
    Davy Kelly
    Ranch Hand
    Posts: 384
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Vicken,

    int k = 0;
    k = k++ + ++k + k++;

    as I see it is :
    k++ is 0 (at the moment add 1 at end of statement)
    +
    ++k is 1
    +
    k++ is 1 (because of last preincrement ++k but add 1 at end of statement)
    so k = 2 (but have to add another 2 because we at end of statement.
    k = 4
    Can I compile it now to see if I am correct???
    Davy
     
    Vicken Karaoghlanian
    Ranch Hand
    Posts: 522
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Davy, your output is correct, but you missed the evaluation order with a big time.
    Here is what really happens:
  • The expression is evaluated from left-to-right
  • 'k++' increment k (Which is zero) thus becoming 1, but returning 0
  • '++k' increment k (Which is one) then returning 2
  • 'k++' increment k (which is two) thus becoming 3, but returning 2
  • The final order of the evaluation is : 0 + 2 + 2
  • Finally 4 is assigned to k


  • Not actually what you had in mind, right? Nice try though.
     
    Davy Kelly
    Ranch Hand
    Posts: 384
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    thank you for correcting me,
    so what your saying is, that when the k++ happens, it increments the value k, after the next +, so therefore it increments before the end of the statement!
    Davy.
    P.s. I will test this out, the wy you look at it, and the way i look at it, to see if i get consistantly same or different output.
     
    Davy Kelly
    Ranch Hand
    Posts: 384
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    hey Vicken,
    cheers, for correcting me, just done a wee program, to see how you looked at it, this is what I done;

    7 different increments in the same line, this is how i looked at it in a linear way from left to right.
    k = 0
    k++ = 0 (but increment to one after)
    ++k = 2 (used the last increment and the preincrement)
    ++k = 3 (preincrement)
    k++ = 3 (post increment, gets value 3 then adds 1 later)
    k++ = 4 (post increment, gets value 3 then adds 1 later)
    ++k = 6 (used the last increment and the preincrement)
    k++ = 6 (post increment, gets value 3 then adds 1 later but does not really get added)
    so the value k = 0 + 2 + 3 + 3 + 4 + 6 + 6 which is
    24
    I think i get it better now, correct me if I am wrong, but this week has been hell, I am soo tired I can't think straight
    Davy
     
    Vicken Karaoghlanian
    Ranch Hand
    Posts: 522
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Congratulations!!! You hit the jackpot.
    To simplify things even more.
  • Preincrement operator : increment then return value.
  • Postincrement operator: return value then increment.


  • Your week being hell!!! You should see mine, I have my exam set next week, I haven't slept for... let me see two weeks maybe. I arrive late to my work place almost every morning, my boss is this close to fire me.
    It is really hard to study and work at the same time.
     
    Davy Kelly
    Ranch Hand
    Posts: 384
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Vicken,
    I read you loud and clear mate, I too have my exam next monday, and I have an interview to prepare for for this wednesday for promotions in the work (nothing to do with IT) and I am working full time 7 days a week. but at least i get a few days off after the 7.
    Good luck with your exam, mate
    Davy
     
    Don't get me started about those stupid light bulbs.
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic