10

I wonder why first code output is 000 while the second one is 123

first one:

int z=0;
    while(z<4)
    {
       z=z++;
       System.out.print(z);

    }

second one :

int z=0;
int x=0;
    while(z<5)
    {
       x=z++;
       System.out.print(x);

    }

what is the different between these two codes , why the first block do not increase the value of the z ?

2
  • 3
    The first code will loop forever and the second will print 01234 Commented Aug 4, 2011 at 17:18
  • @The Scrum Meister: that error stopped me from answering :( Commented Aug 4, 2011 at 17:19

9 Answers 9

13

z=z++ is a programmer's error -- what it does is increment z and then set z to its old value -- as a result it overwrites z with its old value and hence undoes the increment.

Sign up to request clarification or add additional context in comments.

2 Comments

To be more precise, it's merely a logic error. Obviously it will compile and run just fine (although in the example provided, it will result in an infinite loop).
Yes, I was looking for the right adjective to use with the word error. ("semantic error" or "programmer error")
8

The increment operator already increments z, you don't have to assign the return value back to z.

z++

Is a post increment. It returns z and AFTER that it increments z. In your first sample, you are basically just assigning 0 to z and your loop shouldn't end.

In your second sample, you are assigning the old value of z to x and then increment z. This means that you don't start to increment 0 again like in the first example, but when z reaches 5 (so z<5 is false), z is 5 and x is 4 because of the post increment.

Comments

4

When you use the post-increment operator, you don't need to assign the result back to the variable.

That is, your code should look like this:

int z=0;
    while(z<4)
    {
       ++z;
       System.out.print(z);

    }

In Java, the operation returns the value of z BEFORE the increment (while incrementing the variable behind the scenes afterwards), and that value is then RE-assigned to z. That's why it never changes.

The pre-increment operator will do the increment and return the NEW result, so you'll get what you expect:

int z=0;
    while(z<4)
    {
       z=++z;
       System.out.print(z);

    }

This will print 1234.

3 Comments

...of course, the correct program would just do z++; or ++z; and not z=++z;, but good illustration of the difference.
I thinks its best answer tanx
@shanky: Thanks-- if you think so, feel free to click the check mark under the voting area, on the left side of the answer :-)
4

Remember this, Java evaluates your expressions right to left (just like C and C++),

So if your code reads

z = z++

then if z is 0 before this line is executed, what happens is:

  1. z++ is evaluated as an expression, returning the value 0
  2. Then z is incremented because of the ++ operator, and it has the value 1.
  3. Now the z on the left is assigned a value z = (value returned by z++)
  4. Since the value returned by z++ was 0, z is reset to 0.

The important thing to note is that the result of the assignment inherent in z++ is evaluated before the z variable on the left is updated.

Comments

3

I think this will give you a pretty good explanation.

Consider this class:

public class T
{
    public void f() {
    int count = 0;
    count = count++;
    }
}

This is the associated byte code:

public void f();
  Code:
   0:   iconst_0
   1:   istore_1
   2:   iload_1
   3:   iinc    1, 1
   6:   istore_1
   7:   return
}
  1. iconst_0 loads the constant 0 onto the stack (this is for assigning the variable count with value 0
  2. istore_1 stores stack value (0 right now) into variable 1
  3. iload_1 loads int value from variable 1 (0 right now) onto the stack
  4. zinc 1, 1 increments by 1 variable 1 (count = 1 right now)
  5. istore_1 stores stack value (0 right now from step #3) into variable 1
  6. return

Now it should be pretty clear how count = count++ gets compiled in Java.

Comments

1

It's because you are assigning the value of z with a postfix operator.

http://download.oracle.com/javase/tutorial/java/nutsandbolts/operators.html

int z = 0;
i = z++; // i equals 0
x = ++z; // x equals 2

Postfix operators will increment the value of z after assignment of i.

Unary operator ++ will increment the value of z before assignment of x.

Think of it as ++ before z as +1 before assignment, ++ after z as +1 after assignment.

1 Comment

I think this is the right answer, but you probably ought to walk readers through what it means, in particular that the z++ returns a value, and the assignment uses the value.
0

The first one is probably better written as

int z=0;
while(z++<4)
{
   System.out.print(z);
}

or

int z=0;
while(z<4)
{
   z = ++z;
   System.out.print(z);
}

The pre-increment here is important because it will increment and then assign. Rather than assign and then increment - which has no effect other than resetting to 0 in your first example.

Since when you do z=z++ it will reassign the old value back to z thus leading to an infinite loop.

The second one will end because you are not reassigning back to z:

int z=0;
int x=0;

while(z<5)
{
    x=z++;
    System.out.print(x);
}

This will print 1234.

Comments

0

If you're writing anything like foo = foo++, you're doing it wrong. In general, if you see any expression like x = x++ + ++x; something is seriously wrong. It's impossible to predict how expressions of that sort are evaluated. In languages like C, such expressions can be evaluated as the implementer desires.

I'd strongly recommend playing around with the ++ operator because you're bound to encounter it when you read code.

As others have pointed out, x++ is the postfix operator and ++x is a prefix operator.

int x = 0;
int y = x++; // y=0, x=1
int z = ++x; // z=2, x=2

Note that the values of y, z and x are what they are only after the expression is evaluated. What they are during execution is undefined.

So if you see code like foo(x++, ++x, x), run for the hills.

Your own problem is more succinctly written:

for (int z=0; z<4; ++z) {
    System.out.print(z);
}

The above code has the advantage that the variable z is scoped within the for loop, so it won't accidentally collide with some other variable.

Comments

-1
z=z++;

This means first assign the value of z (which is in right position) to the z (which in left position), then do the increment in right z (which is of no use).

4 Comments

Its simple written in one line, so that user can understand easily. But I think its confusing you, so you should do an exercise and learn.
Its simple written in one line, so that user can understand easily. But I think its confusing you, so you should do an exercise and learn. The above comment is for 'Duncan'.
@Duncan: If there is nothing new that doesn't means you should downvote and answer is also not wrong. So I would recommend you to don't be over smart always, and you should read and understand.
@Duncan: If you want to be a good man, you should be a good listener and reader, but you are not. You are over-confident and always under-estimate others. (That you proved). I hope you will understand this one day. Good Day & Good Bye

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.