# Code Experiment - Why the change?

• October 19th, 2013, 02:10 PM
Skywola
Code Experiment - Why the change?
This code is some code that I started out with experimenting, from a bit of code that I
found in OCA Java SE 7 Certification Guide by Mala Gupta. What puzzles me is the
output. My question is why the difference? All of the variables that are used in the
two expressions are the SAME VALUE. I am using Eclipse for my IDE, but I tried it on
the command line and got the same results. Is it a bug?

Code :

``` int a = 0; int b = 0; int c = 0; int d = 0; int e = 0; int f = 0; a = 10; f = a++ + a + a-- - a-- + ++a; System.out.println("out " + f);   a = 10; b = 10; c = 10; d = 10; e = 10; f = 0; f = a++ + b + c-- - d-- + ++e; System.out.println("out " + f);   // Yes, the two are set up the same! f = a++ + b + c-- - d-- + ++e; f = a++ + a + a-- - a-- + ++a;```

The result is:
out 32
out 31

????

Ok, so I just simplified it . . .

Code :

``` int a = 0; int b = 0; int f = 0; a = 10;   f = a++ + a; System.out.println("out " + f);   a = 10; b = 10; f = 0; f = a++ + b; System.out.println("out " + f);```

Result:
out 21
out 20

It has got to be a bug . . . . :bz
• October 19th, 2013, 02:24 PM
GregBrannon
Re: Code Experiment - Why the change?
It's sleight of hand. You're told the equations are the same, and from that you might conclude that the variables have the same value at each point in the equations (the distraction), but do they?

I don't understand what tricks like this have to do with certification of anything, but maybe that's why I'm not certified.
• October 19th, 2013, 02:34 PM
Ubiquitous
Re: Code Experiment - Why the change?
Are they the same values? They may have been initialized with the same initial value but does it remain like so?

Code :

```int a = 5; System.out.println(a); a++; System.out.println(a); a--; System.out.println(a);   // While at it why not try this as well so you can see something else that you may experience in the future. int b = 10; System.out.println(b++); System.out.println(b);```
• October 19th, 2013, 02:45 PM
Skywola
Re: Code Experiment - Why the change?
Ok, I see, said the blind man . . . .

Code :

```  a = 10; System.out.println("a = " + a); f = a++ +a; System.out.println("a =" + a + " a = " + a);   System.out.println("out " + f);   a = 10; b = 10; f = 0; System.out.println("a =" + a + " b = " + b); f = (a++) +b; System.out.println("a =" + a + " b = " + b); System.out.println("out " + f);```

The "a" is being incremented on the top expression . . . because it is "a"!!!!
"b" is not being incremented, because it is not "a"!!!
Duuuhhhh