# expression

here is the following statement

i = 5;
j = i++ - ++i;

What are the possible values of j in Java?

My guess would be 0 because if I increase i then subtract i then i should get 0, am I right? It should be the same as in C++
###### Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Commented:
Hi shahrine99,

i = 5;
j = 5 - 6;

In the end j is -1, i is 7

Joe P
0
Commented:
Oh actually I agree. 0
j = 6-6
i = 7

j should be 0 ur right =)

Joe P
0
Commented:
Your right because ++i increments i before the expression is evaluated, and since they are both i they both become 6 then you get 6-6.  The i++ then takes affect after the expression is evaluated.

Joe P
0
Software EngineerCommented:
Well, since i++ should increment after the reference, and ++i should increment before the usage:

i = 5;
j = 5 - 5;

j = 0;

whereas i == 5

I agree...
0
Author Commented:
ok cool thats in Java...what about in c++?
0
Associate Director - Product EngineeringCommented:
I think in Java, it should be -2. Because in i++, the value of 'i' which will be used will be 5. Then 'i' will be incremented to 6. Then in ++i, 'i' will be incremented again to 7 and then used. So you get 5 - 7 = -2.
0
Associate Director - Product EngineeringCommented:

It will vary across platforms. In some cases it will be -2, in some cases it might be 0 because C++ might use stacks for this and the ++i might get evaluated before i++ in certain cases. We are not supposed to answer that ;-) ask in the C++ topic area.
0
Commented:
This is homework, isn't it?

And, why didn't you simply try this yourself?

;JOOP!
0
Commented:
mayankeagle:
I was taught that ++i inrecrements i Before the statement is evalutated.
I was taught that i++ inrecrements i After the statement is evaluated.
When you said -2 that implies that it happens in real time moving through the statement and I am not too sure that is right.
I of course have not coded this, I'm resisting all urges =)
0
Commented:
mayankeagle is right :

i = 5;
j = i++ - ++i;

in Java is eqv to (java byte code use stack)
// i++
push value of i      (stack: 5 )
increment i  (i=6)
// ++i
increment i  (i=7)
push value of i      (stack: 5 7 )
// -
substract           (stack: -2)
// j=
store into j

It should be the same in most C++ compiler.
You can get different result if you use older C++ compiler or non-standard ones.
0

Experts Exchange Solution brought to you by

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Commented:
0
Commented:
B-Homework.
0
Associate Director - Product EngineeringCommented:
>> I was taught that i++ inrecrements i After the statement is evaluated.

Not the entire statement, just the expression or the sub-expression where it is used. Expect it to evaluate after the first usage.
0
Associate Director - Product EngineeringCommented:
>> Expect it to evaluate

Meaning expect it to 'change the value'
0
###### It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.