Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

# Thread: Code Experiment - Why the change?

1. ## 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?

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

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

2. ## 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.

3. ## 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?

Do this in your compiler:
```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);```

4. ## The Following User Says Thank You to Ubiquitous For This Useful Post:

Skywola (October 20th, 2013)

5. ## Re: Code Experiment - Why the change?

Ok, I see, said the blind man . . . .

```
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