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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Ron McLeod
• Rob Spoor
• Tim Cooke
• Junilu Lacar
Sheriffs:
• Henry Wong
• Liutauras Vilda
• Jeanne Boyarsky
Saloon Keepers:
• Jesse Silverman
• Tim Holloway
• Stephan van Hulst
• Tim Moores
• Carey Brown
Bartenders:
• Al Hobbs
• Mikalai Zaikin
• Piet Souris

# Sybex OCA Java 8 Study Guide

Don Yazurlo
Greenhorn
Posts: 3
• • Number of slices to send:
Optional 'thank-you' note:
• • Hi,  Can someone check this out? I believe there is a discrepancy between the Order of operator precedence table on page 52 and the solution on page 59.
On page 52 is Table 2.1 Order of operator precedence.   The table has the following:
Post-unary operators        expression++, expression--
Pre-unary operators ++expression, --expression

On page 59 is the following example of how to resolve the expression.
int x = 3;
int y = ++x * 5 / x-- + --x;

So, how would you read ths code?  First, the x is incremented and returned to the expression, which is multiplied by 5, We can simplify this:
int y = 4 * 5 / x-- + --x;

I believe that either the table is incorrect or the first step in resolving the expression would've been to substitute the three for the x--
y = ++x * 5 / 3 + --x;

// Next apply the post-unary operator (--) to X.
x now has a value of 2

// Next apply the first pre-unary operator ++x and you have the following expression
y = 3 * 5 / 3 + --x;

// Last apply the second pre-unary operator to --x and you have the following expression
int y = 3 * 5 / 3 + 2;
x now has a value of 2

int y = 3 * 5 / 3 + 2;

int y = 15 / 3 + 2;

int y = 5 + 2;

y = 7;
x = 2;

Liutauras Vilda
Sheriff Posts: 8064
569    • • Number of slices to send:
Optional 'thank-you' note:
• • Don Yazurlo wrote:So, how would you read ths code?  First, the x is incremented and returned to the expression, which is multiplied by 5, We can simplify this:
int y = 4 * 5 / x-- + --x;

I believe that either the table is incorrect or the first step in resolving the expression would've been to substitute the three for the x--
y = ++x * 5 / 3 + --x;

No, where you typed in 3, there really supposed to be 4 as there is used post-decrement, meaning, you place current value into expression and only then decrement.

So the whole expression evaluates next:
int x = 3;
int y = ++x * 5 / x-- + --x;

int y = 4 * 5 / 4 + 2;
y = 20 / 4 + 2;
y = 5 + 2;
y = 7;
x = 2; Liutauras Vilda
Sheriff Posts: 8064
569    • 1
• • Number of slices to send:
Optional 'thank-you' note:
• • It is really easy to get it right if you think about it simpler than are described in most of the tutorials.

Two rules only:
1. if you see --x or ++x that means, you decrement/increment first, and only then place value into expression
2. if you see x-- or x++ that means, you place current value into expression, and only then decrement/increment Don Yazurlo
Greenhorn
Posts: 3
• • Number of slices to send:
Optional 'thank-you' note:
• • I agree with your last post but which takes precedence when there in the same statement?  According to page 52 of the OCA Study Guide the post-unary expression (x++ and x--) takes precedence over the pre-unary expression (++x and --x).  Is that correct?

Liutauras Vilda
Sheriff Posts: 8064
569    • • Number of slices to send:
Optional 'thank-you' note:
• • Book in several places suggests to write lots of code snippets to verify understanding.

What have you found out after trying? Don Yazurlo
Greenhorn
Posts: 3
• • Number of slices to send:
Optional 'thank-you' note:
• • Using a debugger, I tried to step into the statement int y = ++x * 5 / x-- + --x;  Unfortunately, the debugger does not step into the resolution of each unary operator.  Do you have any suggestions on how to visualize the sequence each of the unary operators are resolved in the above statement?

Jeanne Boyarsky
author & internet detective Posts: 40746
827   • • Number of slices to send:
Optional 'thank-you' note:
• • Don Yazurlo wrote:Using a debugger, I tried to step into the statement int y = ++x * 5 / x-- + --x;  Unfortunately, the debugger does not step into the resolution of each unary operator.  Do you have any suggestions on how to visualize the sequence each of the unary operators are resolved in the above statement?

My suggestion is to build it up. First understand why this understands what it does.

Then this one should be easy

Then this one is a nice head scratcher

But once you understand it, you are ready for the full statement!  Campbell Ritchie
Marshal Posts: 73979
332
• • Number of slices to send:
Optional 'thank-you' note:
• • Jeanne Boyarsky wrote:. . . Then this one is a nice head scratcher . . .

Assuming there is no overflow error, what is the relationship of the evaluation of the expression to the original value of x?  With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
• 