Win a copy of OCP Java SE 8 Programmer II Exam Study Guide this week in the OCP forum!
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# Can't understand logic

Ranch Hand
Posts: 58
1
Could anyone explain please why after

a equals 2?

What is the first step in calculating, second etc..

Sheriff
Posts: 4737
131
Is there a declaration for a?

Oleksii Movchan
Ranch Hand
Posts: 58
1
Knute Snortum it works for any a. There obviously is a declaration before since it's Java

Knute Snortum
Sheriff
Posts: 4737
131
• 1
Assuming int a = 0;

1) use a, then decrement, so 0, then a == -1
2) decrement a, then use it, so -2
3) 0 - -2 = 2, so a = 2

Knute Snortum
Sheriff
Posts: 4737
131

Aleksey Movchan wrote:Knute Snortum it works for any a.

Really?  Work it out for a = 5;

Oleksii Movchan
Ranch Hand
Posts: 58
1
Got it. Thank you!

Rancher
Posts: 1299
36
• 1
As long as a has a value is not near MAXVALUE or MINVALUE the result is always the same for this and a equals 2, for int related primitives that is.
I kind of know the answer, but I can not articulate it too well. I'll give it a try:
• Whenever you do a post fix operation like a-- or a++ then you first evaluate a for the equation then do the post fix operation.
• Whenever you do a prefix operation like --a or ++a then you first do the prefix operation then evaluate a for the equation.
• When you have both post fix and prefix occurring in the same equation then you evaluate the values left to right.

•
Oleksii Movchan
Ranch Hand
Posts: 58
1

Knute Snortum wrote:
Really?  Work it out for a = 5;

Try it ;)

Knute Snortum
Sheriff
Posts: 4737
131
Ah yes, you are correct!

Marshal
Posts: 58319
178
If you go through the this FAQ, I think it will give you an explanation. Knute is right; you must have an initial value for your int otherwise it might not compile. But I see what you mean; it will print 2 from any starting value. Even if there is an arithmetic overflow:-

java Minus2Demo 1 2 69 4 2147483647 -2147483648 2147483646 -2147483647
2
2
2
2
2
2
2
2

Rancher
Posts: 2451
80
Well, the outcome isn't very surprising. The right had side boils down to: a - (-1 + (a-1)).
And since MIN - 1 = MAX, underflow makes no difference.

Without using a pc: what is the outcome of: a = --a - a--? And does underflow make a difference?

Campbell Ritchie
Marshal
Posts: 58319
178
• 1

Pete Letkeman wrote:As long as a has a value is not near MAXVALUE or MINVALUE . . .

Please try values close to those extreme values; when I tried it earlier I got 2 even for extreme values with overflow. I think if you decrement a value twice and then subtract you will get 2 irrespective of the starting value. Any overflow will cancel itself out.

. . . first evaluate a for the equation then do the post fix operation. . . .

I think that is a bit too much of an oversimplification. I think it is more accurate to say that there are two values: the value of i alone and the value of the whole expression i++;In the case of postincrement, the value of i++ is equal to the old value of i, and that causes no end of confusion to beginners. The value of i is hidden where you cannot find it until the next expression.
In the case of preincrement, the value of ++i is equal to the new value of i, and that seems to be really easy to understand.
You are 100% right about left to right.

I was taught that underflow only occurs with floating‑point arithmetic; what we are experiencing is overflow in one direction or the other.

 Consider Paul's rocket mass heater.