• 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
  • Ron McLeod
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Rob Spoor
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Carey Brown
Bartenders:

k=k++; // please check my thinking?

 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi all
been wondering about this over the weekend.
int k=6;
k = k++;
System.out.println(k);
prints
6
not 7. i wonder if someone could clarify? i can see the postfix operator is lower precedence than = so the psotix operation happens after the assignment which basically means k = k. what i am not sure about is why the the value of the expression (k++) == 7 is discarded. IOW what is "special" about the postfix operator in the expression k=k++ that means that k is not incremented? this code
int k = 6;
k++;
System.out.println(k);
prints
7
so clearly the k= part is involved. my guess is that the postfix is being discarded but i can't quite see why.
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please check out the following discussions about the same topic:
https://coderanch.com/t/190825/java-programmer-SCJP/certification/Array
https://coderanch.com/t/239576/java-programmer-SCJP/certification/result
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by peter greaves:

int k=6;
k = k++;
System.out.println(k);
prints
6


Hi,
k++ returns the current value of k and then increments it.
++k increments k and then returns the incremented value.
When u write k = k++; things happen in the following sequence:
1 - the current value of k is returned (6).
2 - k is incremented, and its value becomes 7.
3 - the assignment is executed, and the value returned by the expression (6) becomes k's value.
Thus, when u print out the value of k, you get a 6, not a 7!!
Regards,
Jonathas d-_-b
 
Author
Posts: 201
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Could not have said it any better.
 
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jonathas Carrijo:

When u write k = k++; things happen in the following sequence:
1 - the current value of k is returned (6).
2 - k is incremented, and its value becomes 7.
3 - the assignment is executed, and the value returned by the expression (6) becomes k's value.
Thus, when u print out the value of k, you get a 6, not a 7!!


The above sequence is nearly correct, but I would like to suggest a minor modification.
  • The expression, k++, is evaluated and the result, 6, is pushed onto the stack.
  • The value of k is incremented to 7.
  • The previous value of k, 6, is popped from the stack and returned as the result of the expression.
  • The returned value of the postfix expression, 6, is assigned to k.
  •  
    Jonathas Carrijo
    Greenhorn
    Posts: 25
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thanks, Dan!
     
    With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    reply
      Bookmark Topic Watch Topic
    • New Topic