# Thread: question about the operator ++

1. Member
Join Date
Jan 2010
Posts
8
Rep Power
0

## question about the operator ++

hi, i came across the next code:

x = 1;
y = ++x;
System.out.println(y);
prints 2

x = 1;
y = x++;
System.out.println(y);
prints 1

what is the reason for the diffrent result of constant y?

2. Google.. postincrement and preincrement operators :rolleyes:

3. what is the reason for the diffrent result of constant y?

The increment/decrement operators can be applied before (prefix) or after (postfix) the operand. The code result++; and ++result; will both end in result being incremented by one. The only difference is that the prefix version (++result) evaluates to the incremented value, whereas the postfix version (result++) evaluates to the original value. If you are just performing a simple increment/decrement, it doesn't really matter which version you choose. But if you use this operator in part of a larger expression, the one that you choose may make a significant difference.

The following program, PrePostDemo, illustrates the prefix/postfix unary increment operator:

class PrePostDemo {
public static void main(String[] args){
int i = 3;
i++;
System.out.println(i); // "4"
++i;
System.out.println(i); // "5"
System.out.println(++i); // "6"
System.out.println(i++); // "6"
System.out.println(i); // "7"
}
}

for more details, see here

4. But if you use this operator in part of a larger expression, the one that you choose may make a significant difference.
Exactly.. try this. if the above example by j2me64 has clarified the usage of post/pre increment operators.

Java Code:
```class NewClass {
public static void main(String[] args) {
int b=5; int c= (b++) + (++b) + (++b) + (++b);
System.out.println(c);
}
}```

5. Originally Posted by Stephen Douglas
Exactly.. try this. if the above example by j2me64 has clarified the usage of post/pre increment operators.

Java Code:
```class NewClass {
public static void main(String[] args) {
int b=5; int c= (b++) + (++b) + (++b) + (++b);
System.out.println(c);
}
}```
Don't ever do that; it may be defined in Java but it causes undefined behaviour in both C and C++; besides, expressions like the above are only used in pathetic examples. btw. you don't need brackets there.

kind regards,

Jos

6. Senior Member
Join Date
Feb 2010
Location
Waterford, Ireland
Posts
748
Rep Power
7
Originally Posted by dardar
hi, i came across the next code:

x = 1;
y = ++x;
System.out.println(y);
prints 2

x = 1;
y = x++;
System.out.println(y);
prints 1

what is the reason for the diffrent result of constant y?
++ in java is the equivaliant of saying the variable plus 1. so for e.g.

Java Code:
```int x=7;
int y= ++x;```
would mean increment x and then store its current value in the variable y, which would be 8.

the other is this
Java Code:
```int x=7;
y=x++;```
which would mean assign y the current value of x i.e.(7) and then increment x.

both examples could be written in longer terms as:

assign after incrementing
Java Code:
```int x=7;
int y= x+1;```
and.. assign before incrementing.
Java Code:
```int x=7;
int y= x;
x=x+1;```

7. Moderator
Join Date
Apr 2009
Posts
13,075
Rep Power
23
Originally Posted by al_Marshy_1981
assign after incrementing
Java Code:
```int x=7;
int y= x+1;```
I think you mean:
Java Code:
```int x=7;
x = x + 1
int y= x;```
</pedant-mode>:)

8. Member
Join Date
Jul 2010
Posts
16
Rep Power
0

## question about the operator ++

When you use

int x =1;
int y= ++x;
it's mean
x= 1;
x= x+1;
y = x;
first increment the value of the x the assign that value to y.

when you use following format

int x=1;
int y= x++;
then it's mean
x=1;
y=x;
x= x+1;

first assign value to y. then executing next line x value will be increased.

9. Member
Join Date
Aug 2010
Posts
1
Rep Power
0
what if,

int x=1;
x=x++;

what value would x hold now 1 or 2? It holds 1 after execution of x=x++. why is it so?

10. int x=1;
x=x++;
Java Code:
`x = 1`
I'd like this explained as well!

11. Originally Posted by Sno
int x=1;
x=x++;
Java Code:
`x = 1`
I'd like this explained as well!
The postfix operator expression ++ has a value, which is the old value of x and it has as a side effect that the operator increments the value of its operator x. So, in baby steps:

x++ has the value 1 (remember this value)
x++ increments the value of x so x == 2 now
x= value of postfix ++ operator expression (see above)

so x= 1 (and for a very short period of time it had the value 2)

kind regards,

Jos

ps. here's a nice code snippet that explains it all:

Java Code:
```public class TrickyStuff  {

private static int x= 1;

public static void main(String[] args) {

public void run() {
while(x == 1);
System.out.println( "x != 1" );
System.exit(0);
}
} ).start();

System.out.println("x= "+x);
while(true) { x= x++;}
}
}```
Last edited by JosAH; 08-16-2010 at 02:44 PM.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•