Ros Bain

Greenhorn

Posts: 4

posted 11 years ago

In many of the mock SCJP exams the following type of question occurs

int x, a = 6, b = 7;

x = a++ + b++;

After execution of the code fragment above what are the values of x,a and b

The answer given is always

a = 7, b= 8 and x = 13

This is because x is evaluated as a+b and then a++ and b++ are evaluated.

I don't understand why!

According to the operator precedence rules in my java manual unary operators (++) take precedence over arithmetic operators (+).

Is there some other special rule which applies for this case - it appears that the + operator is taking precedence over the ++ operator.

int x, a = 6, b = 7;

x = a++ + b++;

After execution of the code fragment above what are the values of x,a and b

The answer given is always

a = 7, b= 8 and x = 13

This is because x is evaluated as a+b and then a++ and b++ are evaluated.

I don't understand why!

According to the operator precedence rules in my java manual unary operators (++) take precedence over arithmetic operators (+).

Is there some other special rule which applies for this case - it appears that the + operator is taking precedence over the ++ operator.

posted 11 years ago

But, what that doesn't state is the x++ is sort of like calling a function with a return value, and that is the old value of x, not what x++ evaulates to.

so

int x = 6;

int y = 7;

int a = x++ + y++;

so x = 6 then x++ returns the old value of x which is 6 though after it it will be 7, since y = 7 then y++ returns the old value of y which is 7 though after it it will be 8.

so x = 7, y = 8 and a = 13.

Mark

so

int x = 6;

int y = 7;

int a = x++ + y++;

so x = 6 then x++ returns the old value of x which is 6 though after it it will be 7, since y = 7 then y++ returns the old value of y which is 7 though after it it will be 8.

so x = 7, y = 8 and a = 13.

Mark

Ros Bain

Greenhorn

Posts: 4

Stan James

(instanceof Sidekick)

Ranch Hand

Ranch Hand

Posts: 8791

posted 11 years ago

Another good answer to this dilema is "Don't do that!" This is a useful learning exercise as you showed by coming up with the ++a + ++b (good work, by the way) but not something I'd want to run across on a dark night. Even the most experienced Java users find mixing unaries and other operators takes a few too many brain cycles to process. Make it easy for humans first and foremost!!

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi

posted 11 years ago

Ros Bain == ashok reddy devaram ???

http://www.coderanch.com/t/251647/java-programmer-SCJP/certification/Why-so

http://www.coderanch.com/t/251647/java-programmer-SCJP/certification/Why-so

*I thought this looked terribly familiar...*
Ros Bain

Greenhorn

Posts: 4

posted 11 years ago

int x=3;

int y = x++ + ++x;

1) evaluate x++, and push the value onto the stack

vars: {y, 3} stack: {3}

2) increment x

vars: {y, 4} stack: {3}

3) evaluate ++x and push the value onto the stack

vars: {y,4} stack: {4}

4) increment x

vars: {y, 5} stack: {4}

5) add the value of x from the stack to the value of x from the stack, and store the result in y

vars: {8, 5} stack: {}

So y is 8 and x is 5

I would definitely not recommend writing this sort of code as it's confusing for people to understand. For the java exam you need to be able to interpret code statements like this in case you should come across them in other peoples code.

(By the way Ros Bain != ashok reddy devaram

However I notice he has a very very similar taste in questions to me - very curious!)

The information about the use of the stack was very helpful.

Thanks for all your help on this.

int y = x++ + ++x;

1) evaluate x++, and push the value onto the stack

vars: {y, 3} stack: {3}

2) increment x

vars: {y, 4} stack: {3}

3) evaluate ++x and push the value onto the stack

vars: {y,4} stack: {4}

4) increment x

vars: {y, 5} stack: {4}

5) add the value of x from the stack to the value of x from the stack, and store the result in y

vars: {8, 5} stack: {}

So y is 8 and x is 5

I would definitely not recommend writing this sort of code as it's confusing for people to understand. For the java exam you need to be able to interpret code statements like this in case you should come across them in other peoples code.

(By the way Ros Bain != ashok reddy devaram

However I notice he has a very very similar taste in questions to me - very curious!)

The information about the use of the stack was very helpful.

Thanks for all your help on this.