Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

incrementing and decrementing in JAVA

$
0
0
Hey everyone, will i was just reviewing java from the beginning and i came across something that i never understood.
Ok so when you increment something, you use
 
int x=7
x=x++;


and the output is 8.

then why is it that when you do something like this:

int x,y,z;
x=42;
y=x++;
z=++x;



the output is
x=44
y=42
z=44



shouldn't it have been,
x=42
y=43
z=43

why did it skip to 44 instead of 43? and why did it give x the value 44, when i had declared x to be 42?

If someone can please explain how this works.
The same thing happens when you try decrementing it.

int a,b,c;
a=10;
b=a--;
c=--a; 



the output is
1=8
b=10
c=8



instead of a=10, b=9, c=9? like where did the 8 come from and doesn't a=10 and not 8?

thank you for explaining this!

Whoops! mean a=8, not 1=8

View Postmbilal1, on 02 January 2013 - 05:06 PM, said:

Hey everyone, will i was just reviewing java from the beginning and i came across something that i never understood.
Ok so when you increment something, you use
 
int x=7
x=x++;


and the output is 8.

then why is it that when you do something like this:

int x,y,z;
x=42;
y=x++;
z=++x;



the output is
x=44
y=42
z=44



shouldn't it have been,
x=42
y=43
z=43

why did it skip to 44 instead of 43? and why did it give x the value 44, when i had declared x to be 42?

If someone can please explain how this works.
The same thing happens when you try decrementing it.

int a,b,c;
a=10;
b=a--;
c=--a; 



the output is
1=8
b=10
c=8



instead of a=10, b=9, c=9? like where did the 8 come from and doesn't a=10 and not 8?

thank you for explaining this!

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>