lonesome
lonesome

Reputation: 2552

++ operand does not work if var=var++;

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.

Upvotes: 2

Views: 443

Answers (3)

Richard Chambers
Richard Chambers

Reputation: 17573

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.

Upvotes: 1

fgb
fgb

Reputation: 18569

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.

Upvotes: 2

Ramanlfc
Ramanlfc

Reputation: 8354

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

Upvotes: 1

Related Questions