• 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:

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: 4335
39
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: 499
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: 4335
39
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.
 
Bartender
Posts: 1737
63
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.


 
This guy is skipping without a rope. At least, that's what this tiny ad said:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic