• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Sybex - Chapter 3 - Page 81 - Operator Precedence - Paragraph 3

 
Greenhorn
Posts: 11
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jeanne,

The second sentence states that "If two operators have the same level of precedence, then Java guarantees left-to-right evaluation".

This is incorrect as it does not apply to all operators. It does NOT apply to the assignment operators.

The correct statement would be:

"If two operators have the same level of precedence, then Java evaluates the expression left-to-right, except for the assignment operators that are evaluated right to left."

An example is as follows:

int a = 9;
int b = a = 10; //First a is assigned the value 10, Second b is assigned the value of a

Thanks,

Bassam
 
author
Posts: 4278
34
jQuery Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's an edge case I often don't often think of, but yes it is unclear.

Added to Errata: http://www.selikoff.net/ocp11-1

I believe this is the only binary operator to have right-to-left associativity.  All other binary operators are left-to-right.
 
Ranch Hand
Posts: 477
9
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hanumant Deshmukh (OCAJP Associate Java 8 Programmer Certification Fundamentals 1Z0-808) wrote:In other words, almost all of the operators in Java are defined to be left-associative. The only exceptions are the assignment operators (simple as well as compound) and the ternary operator.

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi there!

I noticed that on your website (https://www.selikoff.net/ocp11-1/) you used page 83 instead of 81 for this errata.

Kind regards,
Michael
 
Scott Selikoff
author
Posts: 4278
34
jQuery Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Michael Weidmann wrote:Hi there!

I noticed that on your website (https://www.selikoff.net/ocp11-1/) you used page 83 instead of 81 for this errata.

Kind regards,
Michael



Oh, sorry.  It's page 81 in the 1Z0-815 book, page 83 in the CSG.
 
Saloon Keeper
Posts: 1653
61
Eclipse IDE Postgres Database C++ Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey, this is a corner (edge of an edge) case, but as Charles pointed out that Hanumant Deshmukh mentioned somewhere else, all of the compound assignment operators are right-to-left as well.

jshell> x = 0
x ==> 0

jshell> y = 2
y ==> 2

jshell> z = 3
z ==> 3

jshell> a = x += y += z;
a ==> 5.0

jshell> x
x ==> 5

If it were true that simple = was the only right to left binary operator, x would be == 2 there, but literally all the compound assignment operators are right-to-left as well.

Anyone writing actual code like this should be assigned to nothing but PHP and JavaScript for the rest of their lives, but as written, the errata is still unclear as to whether it applies to all varieties of the compound assignment operators (it indeed does).

I feel like at some point I've seen this tested in someone's mock exam questions somewhere.
Hopefully not on real exam questions, but good to be prepared.


 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic