Noob studying for OCP 1 Java SE 11 exam
[OCP 17 book] | [OCP 11 book] | [OCA 8 book] [OCP 8 book] [Practice tests book] [Blog] [JavaRanch FAQ] [How To Ask Questions] [Book Promos]
Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, TOGAF part 1 and part 2
[OCP 17 book] | [OCP 11 book] | [OCA 8 book] [OCP 8 book] [Practice tests book] [Blog] [JavaRanch FAQ] [How To Ask Questions] [Book Promos]
Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, TOGAF part 1 and part 2
Agree: the original statement was correct.Jeanne Boyarsky wrote:Actually this is not an errata. . . .
No, ++lion is evaluated before the multiplication, because of the left to right rule. If you manage not to get overflow, you get 5 as the result:-. . . So ++lion does get evaluated second.
You can see that the increment to 0 precedes the post‑decrement here:-Campbell's JShell wrote:jshell> int lion = new Random().nextInt(); int tiger = ++lion * 5 / lion--;
lion ==> -306843420
tiger ==> 5
The increment from −1 to 0 occurs before the division and after the post‑decrement, the value of lion-- remains 0, hence the exception. Otherwise the expression would evaluate to *5/−2, i.e. 0.Campbell's JShell wrote:jshell> int lion = -1; int tiger = ++lion * 5 / lion--;
lion ==> -1
| Exception java.lang.ArithmeticException: / by zero
| at (#12:1)
[OCP 17 book] | [OCP 11 book] | [OCA 8 book] [OCP 8 book] [Practice tests book] [Blog] [JavaRanch FAQ] [How To Ask Questions] [Book Promos]
Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, TOGAF part 1 and part 2
This one is more complicated than the previous example because lion is modified two times on the same line. Each time it is modified, the value of lion changes, with different values being assigned to the variable. As you’ll recall from our discussion on operator precedence, order of operation plays an important part in evaluating this example. So how do you read this code?
First, lion is decremented. We can simplify this: Next, lion is incremented with the new value of 3 used in the expression, leading to this:Finally, we evaluate multiplication and division from left to right. The product of the first two numbers is 15. The divisor 3 divides 15 evenly, resulting in an assignment of 5 to tiger. The result is then printed:
[OCP 17 book] | [OCP 11 book] | [OCA 8 book] [OCP 8 book] [Practice tests book] [Blog] [JavaRanch FAQ] [How To Ask Questions] [Book Promos]
Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, TOGAF part 1 and part 2
...but so many people get it wrong. You need something to demonstrate that the left to right rule takes priority over precedences.Jeanne Boyarsky wrote:. . . this example. [is] it's too complicated for the exam. . . . .
I missed out a comma. That should read, “No, postfix operators have a higher precedence than prefix.”Earlier today, I wrote:No postfix operators have a higher precedence than prefix. . . ..
Campbell Ritchie wrote:
...but so many people get it wrong. You need something to demonstrate that the left to right rule takes priority over precedences.Jeanne Boyarsky wrote:. . . this example. [is] it's too complicated for the exam. . . . .
[OCP 17 book] | [OCP 11 book] | [OCA 8 book] [OCP 8 book] [Practice tests book] [Blog] [JavaRanch FAQ] [How To Ask Questions] [Book Promos]
Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, TOGAF part 1 and part 2
I am afraid that isn't quite correct; the value of the variable is decremented, but the value is hidden behind the OLD value, which is the value of the whole expression.Alejandro Vizcaino wrote:. . . 3) lion-- uses it's CURRENT value of 4 in the expression BEFORE decrementing the value by 1: . . .
Your mother is a hamster and your father smells of tiny ads!
The Low Tech Laboratory Movie Kickstarter is LIVE NOW!
https://www.kickstarter.com/projects/paulwheaton/low-tech
|