Diffrence between x++ and ++x? [duplicate] - java

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
Is there a difference between x++ and ++x in java?
I am reading the official Java tutorial and I don't get the difference between postfix and prefix (++x vs x++). Could someone explain?

++x: increment x; the value of the overall expression is the value after the increment
x++: increment x; the value of the overall expression is the value before the increment
Consider these two sections:
int x = 0;
System.out.println(x++); // Prints 0
// x is now 1
int y = 0;
System.out.println(++y); // Prints 1
// y is now 1
I personally try to avoid using them as expressions within a larger statement - I prefer standalone code, like this:
int x = 0;
System.out.println(x); // Prints 0
x++;
// x is now 1
int y = 0;
y++;
System.out.println(y); // Prints 1
// y is now 1
Here I believe everyone would be able to work out what's printed and the final values of x and y without scratching their heads too much.
There are definitely times when it's useful to have pre/post-increment available within an expression, but think of readability first.

++x increments x and then returns the value
x++ returns the value of x and then increments the variable
For example:
int x = 0;
int A = ++x; // A = 1
int B = x++; // B = 1
int C = x; // C = 2

Well, you get enough answers, I'm going just to worry you... Both post- and pre-increment operators can confuse code, so sometimes it is better to use just x+1 then you and other people definitely know what is going on there. Some examples:
int x = 5;
x = ++x;
System.out.println( x ); // prints 6
x = x++;
System.out.println( x ); // prints 6!
x = ++x + x++;
System.out.println( x ); // prints 14!
two last incrementing can be a source of problems to debug then (was watching that few times in my life...). x = x++ - it is evaluated before incrementing... So be careful!

++x is pre-incrementing and x++ is post-incrementing. With post-incrementing the value is increased after evaluation and with pre-incrementing the value is increased before evaluation.

Well, standing alone it's the same. but, if there are other operands involved - ++x will advance x and then apply the other operands, and x++ will first use x and then advance it.

Basically, ++x adds 1 to x before x is evaluated, while x++ adds 1 afterwards. It makes sense if you use it as an argument.
Let's start with x
int x = 3;
If we call System.out.println on x using the infix operator:
System.out.println(++x);
x will first increase to 4 and the println will output, "4". It is pretty much the same if we did:
x+=1;
System.out.println(x);
Let's imagine x equals 3 again. If we call System.out.println on x using the postfix operator:
System.out.println(x++);
It will first output the current value of x, "3", and then increase x. So it's like:
System.out.println(x);
x+=1;
Hope that helps.

Related

Tricky basic java operations [duplicate]

This question already has answers here:
How do the post increment (i++) and pre increment (++i) operators work in Java?
(14 answers)
Closed 3 years ago.
I got these 2 different codes in Java that I can't understand how they produce these specific outputs.
They seemed to work in 2 different logic and I can't figure it out!
int a = 5;
int b = 4;
a -= (b++) + ++a; // For more confusing results replace -= with += and see how the logic changes.
System.out.println(a); // Output: -5
and
int c = 8;
c += ++c;
System.out.println(++c); // Output: 18
How does each situation work and how are they producing these specific outputs?
The major difference here is what post and pre increment are. These determine whether the value is increased before evaluation, or afterward.
Here is how the first case breaks down mathematically:
a = 5 - (4 + 6), which reduces to -5.
Note that a is increased from ++a AKA preincrement before the math is done, however b is calculated as 4. Another thing to note is the a used from the -= uses the original a value, regardless of post or pre increment.
The second equation reduces to this mathematically:
c = 8 + 9 which reduces to 17.
The output prints 18 because your System.out.print(++c) increments it one more time before output due it being preincrement. Note if the print statement used c++, the value would print to 17.
The chart for operator precedence can be found here. Note that assignment has lower precedence than the unary/postfix operators.
It's all about the Operator Precedence in Java. Check that table and figure out which operation takes place first and which last.
It is equivalent to:
int a = 5;
int b = 4;
a -= (b++) + ++a; // => 5 -= (4) + 6
int c = 8;
c += ++c; // => 9 += 9
The main diff is thet:
++a and ++c increments the value and immediately returns it.
b++ returns the value and then increments it.
There is a difference in the order of ++. While both increase the variable, ++a will pass the original value to the operation chain your in middle of; while a++ would pass the new value. So for your first example:
++a --> a is now 6; but the equation is using 5:
a -= (b++) + 5;
b++ --> b is now 5;
a -= 5 + 5;
a -= 10;
? = 5 - 10;
a = a - 5 + 5;
a = 5 - 5 + 5;
a = -10;
(You should have enough to trace the second example).
For a list of operations try this. Some more increment examples are here.
A minimal example is `
int x=3,y=3;
x += ++x;
y+= y++;
at the x x is 7 and y is 6. Precedence alone is not enough to explain the behaviour. Using precedence the second line would be x += (++x), i.e. increment x and return its value, (x is now 4); next we have x+=4 which would return 8.
Instead, it seems better to treat x += w as a short hand for x = x + w, this rewriting happens before evaluation. In our case the rewriting is x = x + ++x interpreted as x = (x + (++x)). So interpreted as
x = ( 3 + (++x))
x = ( 3 + 4 ) x is 4
x = 7 x is 7
A simlar system works to the y equation giving y = 6 at the end.

Priority operators in Java [duplicate]

This question already has answers here:
What are the rules for evaluation order in Java?
(5 answers)
Closed 4 years ago.
For the next code, what it's z? (Java)
int x = 5;
int y = 10;
int z =++x*y--;
The order of priority is: y--, ++x, *, =.
( https://introcs.cs.princeton.edu/java/11precedence/ )
Why after run the code, z = 60 ?
The ++ operator is evaluated before the expression.
i.e.:
int x = 10;
int y = ++x; //y = 11
int z = x ++; // z = 11;
y-- is higher up on the list from your source. However, when the post-decrement happens, it happens after the whole evaluation.
So if you print y after getting the value of z, it will be 9.
And the pre-increment happens first, so ++x becomes 6 within that statement (and obviously multiplies by 10).
See an example in the docs.

When exactly does postfix unary operator happen?

I read a few post regarding unary operator:
What is the Difference between postfix and unary and additive in java
"C - C++" joke about postfix/prefix operation ordering
And a few more.
However, I still don't understand exactly when the value is changed.
For example:
int x = 1;
x = x++;
System.out.print("x = x++ ==> ");
System.out.print(" x = " + x);
System.out.println();
int x = 1;
x = x++ + x++;
System.out.print("x = x++ + x++ ==> ");
System.out.print(" x = " + x);
System.out.println();
The output is:
x = x++ ==> x = 1
x = x++ + x++ ==> x = 3
So in the first block x is assigned to x and afterwards incremented, but the value is never used, otherwise the output would have been x = 2.
In the second block, if I understand correctly, the first x++ is evaluated before the assignment and the second x++ is evaluated afterwards but is never used.
If in the second block both x++ would have been evaluated after the assignment but never used, the output would have been x = 2. If both have been used, the output would have been x = 4.
My IDE also indicated that the first x++ is used, but the second is not used:
So to conclude - I'm still confused about when and how exactly the increment is done.
At the line
x = x++ + x++;
Assuming x = 1, the first x++ returns "1" as the value, and then it increments x to 2. So basically, it's assigning the old value back to x.
The second x++ does the same; it returns the value of x, which is now 2, and only then increments its value to 3 - that value, is not used.
Your code is equivalent to:
tmp = x;
x = x + 1;
tmp2 = x;
x = x + 1; // not used
x = tmp + tmp2;
Links that may help you:
JSL - 15.14.2. Postfix Increment Operator ++
JLS - 15.15.1. Prefix Increment Operator ++
What is x after “x = x++”?

Why does this code goes into infinite loop? [duplicate]

This question already has answers here:
What is x after "x = x++"?
(18 answers)
Why does this go into an infinite loop?
(26 answers)
Closed 9 years ago.
Consider the following code :
public class Game {
public static void main(String[] args) {
int x = 0;
while (x < 1) {
x = x++;
}
}
}
With my compiler this code goes into infinite loop , but why ?
First , we place the value of x into x , and only then we add 1 to x , and afterwards I would expect that 1 < 1 would be false , and the while loop would terminate ...
But no , that doesn't happen .
So what might be the reason for the infinite loop ?
The expression x++ is a post-increment, meaning that it the value of the expression is the old value, 0, which is then assigned back to x, so x is always 0 after x = x++;.
To break out of the infinite loop, don't assign it back to x, leaving the post-increment value of x, 1, intact:
while (x < 1) {
x++;
}
x++ is pretty much equivalent to
some-temporary = x;
x = x + 1;
now use the value of some-temporary
So x = x++; is equivalent to
some-temporary = x;
x = x + 1;
x = some-temporary;
so you should be able to see why the part that adds 1 has no effect.
That is beacause x gets incremented after being used. So the expanded sequence of operations taking place would be:
oldX = x
x++, that is, x = x + 1
x = oldX
oldX being a fictitious variable.
What happens is that x is always assigned oldX, which in our case is 0, resulting in an infinite loop. The x++ part is just ignored.
Solution: replace x = x++ with x++ or, if you really want, x = ++x.
x = x++ doesn't do what you want. Replace it with x++.
x++ is equivalent to x = x + 1, so your assignment doesn't make sense.
More information about this can be found in answers here:
Why does this go into an infinite loop?

Why doesn't x increment after x++? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
What is x after “x = x++”?
In a loop I have:
int x = 0;
while(int x < 10){
x = x++;
}
Why does this not work?
Change x = x++ to just x++. x++ is unary operation and you don't need to use the assignment operation.
Post increment operator uses the value first and then increments.
x=x++;
Here, we assign X to X (zero to zero) and then we increment X to 1 but we never assign it to anything.
you can change to
X=++X;
and this should give you what you want.
Well... try this instead.
int x = 0;
while(int x < 10){
x++;
}
Try converting
x = x++;
to
y = x++; // i.e. y = x, then increment x
x = y;
to understand it.

Categories

Resources