3

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?

Community
  • 1
  • 1
Dr Hydralisk
  • 1,171
  • 4
  • 14
  • 22
  • 5
    http://stackoverflow.com/questions/1094872/is-there-a-difference-between-x-and-x-in-java – stecb Jan 17 '11 at 21:11
  • The answer that was accepted did not explain, he just posted an example (I saw that post before I created my topic). Jon's explanation is what helped me understand. – Dr Hydralisk Jan 17 '11 at 21:27

6 Answers6

12

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

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
5

++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
Gili
  • 86,244
  • 97
  • 390
  • 689
3

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!

Maxym
  • 11,836
  • 3
  • 44
  • 48
  • This reminds me of university C++ test questions, like: Given statement `x = ++x+++++x++`, and knowing `x = 1` at the beginning, what is the value of x after execution of that statement (if it compiles, if not explain why). – Rekin Jan 17 '11 at 21:43
  • yes, this is typical quiz. I noticed that many junior developers, who wanna show their language knowledge, prefer to use different "shortcut" tricks, and only after some time they get experienced that better to write it simple and easy to understand.. Especially when they have to use few languages (like Java and JavaScript), and see that the same type of code can behave differently because of language specification... – Maxym Jan 17 '11 at 21:52
2

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

orlp
  • 112,504
  • 36
  • 218
  • 315
1

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.

Dani
  • 14,639
  • 11
  • 62
  • 110
1

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.

Zach L
  • 16,072
  • 4
  • 38
  • 39