• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

post increment pre increment query output diffrnt then what i am expecting

 
Vishal Hegde
Ranch Hand
Posts: 1077
Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Output:

a=38
I=29

Hows this output possible i mean I am manually pretnding to be the compiler and in that case i get i=18 where am i going wrong
 
Vishal Hegde
Ranch Hand
Posts: 1077
Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Phew I resolved this query manually
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let me guess. Initially you made the common mistake (even made by at least one author of a Java book) of thinking that x = y++; was equivalent to these steps:

1. Store the current value of y in x.
2. Increment the value of y by 1.

Later, you realized that x = y++; is actually equivalent to:

1. Take note of the current value of y.
2. Increment the value of y by 1.
3. Store the original value from step 1 in x.

That is, you thought the post-increment operator did its incrementing as the last step in the statement, after the assignment to the LHS variable. But then you learned that actually, all the incrementing on the RHS is always done before assigning to the LHS variable.

Yes?

 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also note that you cannot get any answer (neither right nor wrong) merely by "manually pretnding to be the compiler". The compiler doesn't do the additions; the runtime does. So you'd have to pretend to be the JVM.
 
Praveen Kumar M K
Ranch Hand
Posts: 256
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff Verdegan wrote:
That is, you thought the post-increment operator did its incrementing as the last step in the statement, after the assignment to the LHS variable. But then you learned that actually, all the incrementing on the RHS is always done before assigning to the LHS variable.

Yes?



This was frankly eye opening...But whats the difference between the two? Since you have 3 steps in the second way, isnt it time consuming AND a waste of a temp variable.
 
Vishal Hegde
Ranch Hand
Posts: 1077
Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff Verdegan wrote:Let me guess. Initially you made the common mistake (even made by at least one author of a Java book) of thinking that x = y++; was equivalent to these steps:

1. Store the current value of y in x.
2. Increment the value of y by 1.

Later, you realized that x = y++; is actually equivalent to:

1. Take note of the current value of y.
2. Increment the value of y by 1.
3. Store the original value from step 1 in x.

That is, you thought the post-increment operator did its incrementing as the last step in the statement, after the assignment to the LHS variable. But then you learned that actually, all the incrementing on the RHS is always done before assigning to the LHS variable.

Yes?



I was aware of the Post increment and pre-Increment.. later on i remembered precedence(as of who is given the high importance).Also i was making an addition error when incrementing it
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Praveen Kumar M K wrote:
Jeff Verdegan wrote:
That is, you thought the post-increment operator did its incrementing as the last step in the statement, after the assignment to the LHS variable. But then you learned that actually, all the incrementing on the RHS is always done before assigning to the LHS variable.

Yes?



This was frankly eye opening...But whats the difference between the two?


In most cases, the practical difference is none. You end up with the same result either way. But in the above code, you'd get very different results.

Since you have 3 steps in the second way, isnt it time consuming AND a waste of a temp variable.


I wouldn't call it a waste. What you get is clear, simple, consistent behavior, and a few bytes and a clock cycle or two is hardly a "waste".
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vishal Hegde wrote:
I was aware of the Post increment and pre-Increment.


So you understand that the following will print 1, and why? That is, you know that x becomes 2 before we do the assignment to the LHS, right?

 
Vishal Hegde
Ranch Hand
Posts: 1077
Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jeff,

As per the precedence post increment or pre increment is given the first priority

Initially the value of x++ is 1 before increment happens, later on it is incremented by 1..

Now 2nd priority is given to assignment hence the original value of x that is 1 is assigned to x(the value assigned before increment is done)
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vishal Hegde wrote:Hi Jeff,

As per the precedence post increment or pre increment is given the first priority

Initially the value of x++ is 1 before increment happens, later on it is incremented by 1..

Now 2nd priority is given to assignment hence the original value of x that is 1 is assigned to x(the value assigned before increment is done)


Yes, that's correct. I just wanted to make sure you understand that. A lot of people don't.
 
Vishal Hegde
Ranch Hand
Posts: 1077
Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff Verdegan wrote:
Vishal Hegde wrote:Hi Jeff,

As per the precedence post increment or pre increment is given the first priority

Initially the value of x++ is 1 before increment happens, later on it is incremented by 1..

Now 2nd priority is given to assignment hence the original value of x that is 1 is assigned to x(the value assigned before increment is done)


Yes, that's correct. I just wanted to make sure you understand that. A lot of people don't.


I am working hard to be like you expertise
 
Praveen Kumar M K
Ranch Hand
Posts: 256
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for clarifying that Jeff, could you be so kind enough to break down the steps for pre-increment too.(couldnt find it on the net)

Another thing, is this increment behaviour "well defined" in Java? I see in many forums, especially those of C++, categorizing the working of increment operators as "Undefined Behaviour" and the solution being the usage of parentheses.Sequence Points . I tried using the parentheses and it made no difference here in Java.

Jeff Verdegan wrote:I wouldn't call it a waste. What you get is clear, simple, consistent behavior, and a few bytes and a clock cycle or two is hardly a "waste".


Maybe waste was too strong a term, I dint mean it to be that way :-)
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Praveen Kumar M K wrote:Thanks for clarifying that Jeff, could you be so kind enough to break down the steps for pre-increment too.(couldnt find it on the net)


That's a bit simpler:


1) Increment x by 1. This is the value of the expression on the RHS.

2) Store the new value of x (the value of the ++x expression) in y.

Another thing, is this increment behaviour "well defined" in Java?


Yes. It is strictly defined that the behavior will be as I described. In C and C++, the order of operations in an expression like x = a++ + ++b + a++ is not defined.

I see in many forums, especially those of C++, categorizing the working of increment operators as "Undefined Behaviour"


The behavior of the operators in a vacuum is defined. What's not defined is the order of operations involving these operators in more complex expressions.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49367
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Praveen Kumar M K wrote: . . . I see in many forums, especially those of C++, categorizing the working of increment operators as "Undefined Behaviour" . . .
They behave differently in C/C++. In fact in C, the behaviour of i++ is not strictly defined; I once tried i = i++; with three different compilers and got different outputs.
But you are writing Java™, where the behaviour of i++ is strictly defined. It’s a different language. Forget all about C/C++ when you are writing Java™.
 
Praveen Kumar M K
Ranch Hand
Posts: 256
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check!

Thanks for the inputs people
 
Campbell Ritchie
Sheriff
Pie
Posts: 49367
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You’re welcome
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic