Ros Bain

Greenhorn
+ Follow
since Nov 16, 2005
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Ros Bain

enum is a new reserved word in 1.5

so if you are very unlucky and your 1.4 code uses this you may need to adjust it.
18 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.
18 years ago
Thanks - I forgot about the importance of the position of the ++

In order for x to evaluate as 15 I would have needed the expression to read

x = ++a + ++b;



18 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.
18 years ago