2

From old times, I have learned that ++ means to incrementally add to the value of an integer. if ++var it will first add one then assign it to the value and if var++ it will add one later.

Here is my question:

int step=0;
if(conditon==true){
    while(!end ){
        step=step++;
    }
}
     System.out.println(step);

The output of this small piece of code will be zero. But if I replace step=step++; with step=++step; then it gives the right answer.

However I am confused that why there is a difference?

EDIT

The difference with the referred answer: There are 2 (or even 3) different variables that are assigned to each other while here step=step++ will result into 0. Based on the answers in that question, it will be different. but it is not. why?

Here is an answer to that question :

int x = 0;
int y = 0;
y = ++x;            // result: y=1, x=1

int x = 0;
int y = 0;
y = x++;            // result: y=0, x=1

acoording to this, step=step++ should add up to step (because both x and y here are same variable which is step) but it does not.

EDIT 2

There is something more confusing. If replacing step=step++ with step++, then it will result into same thing as step=++step which is confusing me even more.

lonesome
  • 2,503
  • 6
  • 35
  • 61
  • 1
    Specifically, this [answer](http://stackoverflow.com/a/12111301/794242) will explain the details – Wand Maker Nov 22 '15 at 09:40
  • @WandMaker no it did not. in those answers, there are 2 different variable that are assigned to each other while here `step=step++` will result into 0. Based on the answers in that question, it will be different. but it is not. why? – lonesome Nov 22 '15 at 10:00
  • 1
    Reread carefully it explain into which bytecode (and therefore in pseudo java code) y = y++ will be translated into. Therefore you have the order of instructions and a clear explanation of the results you get. – benzonico Nov 22 '15 at 10:12
  • @benzonico but eventually it adds one to the `y`. Hence, at the end of my code, same thing should happen to the `step` which it does not. – lonesome Nov 22 '15 at 10:21
  • 1
    you need to break down what the operation is doing. `y = x ++; ` is `int t = x; x++; y = t;` but `step = step++;` is `int t = step; step++; step = t;` so it shouldn't change `step` as it gets overwritten which doesn't happen in the `x` and `y` case. – Peter Lawrey Nov 22 '15 at 10:33
  • 1
    It *doesn't* eventually add one to `y` it adds 1 *before* the assignment. – Peter Lawrey Nov 22 '15 at 10:33
  • @PeterLawrey but `++x is called preincrement while x++ is called postincrement. ` so it should first do the assignment and then add. just like what commonly all programmers doing in a for-loop as `i++`. All in all, do you mean `step++` would give a different result? which it did gives same results as `step=++step`. now I am a little confused. – lonesome Nov 22 '15 at 10:46
  • 2
    @lonesome it is post increment. i.e. after obtaining the value, it is not post everything. The assignment always occurs last. – Peter Lawrey Nov 22 '15 at 11:14
  • @PeterLawrey then I guess I had it all the time wrong in my mind. thought pre and post is regarding to the assignment. Nevertheless, you did not answer the second part of my last comment. – lonesome Nov 22 '15 at 11:36
  • @lonesome `step++` doesn't give a different result. but `step=step++;` or `i=i++` would. – Peter Lawrey Nov 22 '15 at 19:35
  • @PeterLawrey I meant `step=step++` compare to just `step++` gives different result while both are using same incremental operand. – lonesome Nov 22 '15 at 22:51
  • @lonesome one is doing an assignment after the increment and the other is not. The code is not the same so you should expect it to do something different. – Peter Lawrey Nov 23 '15 at 19:04

3 Answers3

2

step++ and ++step by themselves increment the value of step, but they evaluate to different values. step++ evaluates to the incremented value, ++step evaluates to the non-incremented value.

step = step++ increments step and then assigns the non-incremented value to step.

step = ++step increments step and then assigns the incremented value to step.

So step = step++ is incrementing step but then setting it back to its original value making the statement appear to have no effect.

fgb
  • 18,439
  • 2
  • 38
  • 52
  • what do you mean when you say `evaluates to the incremented value` and `evaluates to the non-incremented value`? – lonesome Nov 22 '15 at 13:01
  • @lonesome If step was 1, then after incrementing it the incremented value is 2, and the non-incremented value is 1. This is the value that's used when you assign it or use it in an expression. – fgb Nov 22 '15 at 13:08
1

i striped out your condition into the simple following code :

  int step=0;
  step=step++;           
  System.out.println(step); // will give 0

The reason this gives 0 as output is because the incremented value is never assigned to step.

for step=step++; this is what happens

int temp = 0;
step = temp; // step will get 0
temp = temp +1; // while temp is incremented

in case of ++step the above changes to

int temp =0 ;
temp = temp + 1; // temp gets incremented
step =temp; // step gets the  incremented value
Ramanlfc
  • 8,283
  • 1
  • 18
  • 24
  • What about for `step++`? I mean just `step++` and the result is the same as `step=++step`. My question is **Why?**. – lonesome Nov 22 '15 at 12:18
  • @lonesome i've provided both cases – Ramanlfc Nov 22 '15 at 12:19
  • @lonesome for just step++ , incremented value of `temp` will be assigned to `step` , but if you write `step=step++;` value will get assigned before the increment – Ramanlfc Nov 22 '15 at 12:24
  • In your answer you did not provided when it is only `step++`. So, what about `++step`? does this have same result as `step++`? My another question is that why when we explicitly assign step++ to step, it does not assign it but when we just write step++ it does? is there any particular reason? – lonesome Nov 22 '15 at 12:29
  • @lonesome i provided `++step` in the answer and for just `step++` read the comment – Ramanlfc Nov 22 '15 at 12:36
  • chill out. dont easily get angry. what I was talking is about only `step++` and only `++step` if they are different and also why there is a different between `step=step++` and just `step++`? – lonesome Nov 22 '15 at 12:40
1

The difference in behavior is due to the behavior of two different operators that you are using. The ++ in front of the variable is a prefix operator and the ++ after the variable is a postfix operator. The two different operators modify the variable at two different times during the evaluation of the expression. See this stackoverflow What is the difference between prefix and postfix operators?

When you do an assignment, the operations on the right side of the assignment operator (the equals sign or =) are evaluated and the result is put into the variable on the left side of the assignment operator.

The operations on the right side are evaluated or computed by using various rules of operator precedence. See this stackoverflow Operator precedence in Java.

So the question is when the right side is evaluated, what is the result of the various operations that are to the right side of the assignment operator.

Both the prefix and the postfix operators have almost the highest operator precedence in Java as well as most languages. This means that unless parenthesis are used to modify the sequence of evaluation, prefix and postfix operators will be one of the first operators evaluated. See Java Operator Precedence Table for a short and simple cheatsheet table.

The prefix operator modifies the variable and then the new value of the variable is used in any additional operations. The postfix operator modifies the variable after the current value of the variable is saved as a temporary value.

In other words, the value of a variable used in an expression will be a changed value with a prefix operator and an unchanged value with a postfix operator.

It is easier to see what is happening by using two variables rather than one:

int  a, b;

a=0;
b = ++a;  // increment a and assign the new value of variable a to variable b
b = a++;  // increment a and assign the old value of variable a to variable b

If you use the single variable as you are doing then what happens is that the value of the variable a is changed by the assignment statement to be whatever the right hand side evaluates to. So even though the variable a is modified by the prefix and the postfix operators, the assignment will overwrite the new value with what ever the right hand side evaluates to.

With the prefix operator, the right hand side evaluates to the new, incremented value of the variable a. With the postfix operator, the right hand side evaluates to the old value of the variable a before it is incremented by the postfix operator.

Often times when the desire is to just increment a variable, a prefix or postfix ++ is used in a stand alone statement.

int a = 0;

a = a + 1;   // increment a by adding 1 to the value and assigning new value to a
a += 1;  // increment a by using += operator
a++;   // increment a by using postfix ++ operator
++a;   // increment a by using the prefix ++ operator

In the statements above where the ++ operator is used nothing is done with the result of the operation. So the variable a is incremented however the old value of a from the postfix operator or the new value of a from the prefix operator are not used for anything. The only result of using these operators is that the variable a is incremented.

In other words when a prefix or postfix operator is used there are actually two outcomes or results from the operator. The first result is the change in the variable that is being modified by the operator, also known as a side effect. The second result is the value that is provided to any other operators that may be involved in the calculation. In the case of the prefix operator, this second result is the new value of the variable after the prefix operator is applied. In the case of the postfix operator, this second result is the old value of the variable before the postfix operator is applied.

Community
  • 1
  • 1
Richard Chambers
  • 16,643
  • 4
  • 81
  • 106