• Post Reply Bookmark Topic Watch Topic
  • New Topic
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
  • Devaka Cooray
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Jeanne Boyarsky
  • Tim Cooke
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
Bartenders:

Q about Boyarsky and Selikoff Java 1Z0-808 study guide book

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am studying the book and I am currently on chapter 2. However I found something on page 54 I am not quite sure about when parentheses to override precedence is explained.

First we see the following example:




you first evaluate the 2 * 5 and 3 * 4, which reduces the expression to the following:




Everything makes sense so far. However, in the same section, the book presents the same expression but with the use of parentheses to alter the precedence of operators:


This time you would evaluate the addition operator 10 + 3, which reduces the expression to the following:



Now, here's where I'm encountering some confusion: The book mentions "10 + 3" in the explanation, but within the parentheses, we clearly have "5 + 3." Is this a mistake in the book's explanation?

I went to the Errata table found here: https://www.selikoff.net/java-ocp-8-programmer-ii-study-guide/, but couldn't find any information related to it.

Thank in advance for the help
 
Marshal
Posts: 78698
374
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

George Melo wrote:. . .
. . .

Actually, the Java® way to do it is go from left to right. You first evaluateSince the assignment operator has a low precedence, everything to its right is calculated before the assignment.
Next:-which reduces toThe * operator to the right has a higher precedence, sois calculated, as,which becomesFilling in the ... gives usThe expression now becomes 14 and that is assigned to x.
If you add (), that only changes the order of evaluation when the first ( is reachedNext:-So far, the same as before. When the () appear, however, the expression to the right of is evaluated as a whole expression.
The second () cause their contents to be evaluated, so 3 + 5 becomes 8:-which becomesthen,thenandNo, I can't see anywhere that 10 + 3 appears. Please supply more information about the 10 + 3.

When I wrote ... I meant that part of the expression is only evaluated later, not that it has vanished.
 
George Melo
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


The multiplicative operators (*, /, %) have a higher order of precedence
than the additive operators (+, -). That means when you see an expression such as this:

you first evaluate the 2 * 5 and 3 * 4, which reduces the expression to the following:

Then, you evaluate the remaining terms in left-to-right order, resulting in a value of x of
14. Make sure you understand why the result is 24 as you’ll likely see this kind of operator
precedence question on the exam.
Notice that we said “Unless overridden with parentheses…” prior to Table 2.1. That’s
because you can change the order of operation explicitly by wrapping parentheses around
the sections you want evaluated fi rst. Compare the previous example with the following
one containing the same values and operators, in the same order, but with two sets of
parentheses:

This time you would evaluate the addition operator 10 + 3, which reduces the expression to the following:

You can further reduce this expression by multiplying the fi rst two values within the
parentheses:

Next, you subtract the values within the parentheses before applying terms outside the
parentheses:
Finally, you would multiply the result by 2, resulting in a value of 48 for x

 
Campbell Ritchie
Marshal
Posts: 78698
374
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

George Melo wrote:. . . you first evaluate the 2 * 5 and 3 * 4 . . .

That isn't quite how Java® does it; the left‑to‑right rule ranks above precedence and (...).

This time you would evaluate the addition operator 10 + 3 . . .

You are right; there is no 10 + 3 to be seen anywhere and that has to be a misprint. I think it should read 5 + 3.
 
CLUCK LIKE A CHICKEN! Now look at this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic