开发者

java operator ++ problem

开发者 https://www.devze.com 2023-03-26 11:43 出处:网络
I wonder why first code output is 000 while the second one is 123 first one: int z=0; while开发者_如何学运维(z<4)

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 ?


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.


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.


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.


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.


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.


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.


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.


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.


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).

0

精彩评论

暂无评论...
验证码 换一张
取 消