[HD Pg 129, Sec. 5.1.6 - precedence]

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
OCAJO1
Posts: 221
Joined: Mon Nov 26, 2018 2:43 pm
Contact:

[HD Pg 129, Sec. 5.1.6 - precedence]

Post by OCAJO1 »

Looking at the table of precedence on page 130,

postfix expr++, expr-
unary ++expr, --expr, +expr, -expr, ~, !

How come in Java postfix has higher precedence than prefix?

Thanks

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: [HD Pg 129, Sec. 5.1.6 - precedence]

Post by admin »

Not sure I understand your question. It is the decision of the language designers.
If you like our products and services, please help us by posting your review here.

OCAJO1
Posts: 221
Joined: Mon Nov 26, 2018 2:43 pm
Contact:

Re: [HD Pg 129, Sec. 5.1.6 - precedence]

Post by OCAJO1 »

Oh I figured as much. I just was wondering if there was "as you always say about Java conventions" a logical reasoning behind their decision :)

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: [HD Pg 129, Sec. 5.1.6 - precedence]

Post by admin »

They most likely had a valid reason for it but I haven't seen it documented anywhere. I guess, they continued with the existing "convention" in this case.
If you like our products and services, please help us by posting your review here.

flex567
Posts: 202
Joined: Mon Apr 02, 2018 8:40 am
Contact:

Re: [HD Pg 129, Sec. 5.1.6 - precedence]

Post by flex567 »

This is the example you provided for explaining precedence:

Code: Select all

byte b = (byte)(i + 1);
The Cast operator is in the precedence table, but Parentheses are not in you precedence table nor the Parentheses are explained in the 5 chapter - Using Operators.

At least not in the tables in the chapter
5.1.1 Overview of operators available in Java

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: [HD Pg 129, Sec. 5.1.6 - precedence]

Post by admin »

Because parentheses are not operators as such. They change the precedence of the components of an expression just like the mathematical expressions as mentioned at the start of the section. The book assumes you know BODMAS principle (from mathematics). For example, 2 + 6/2 is 5 but and (2+6)/2 is 4.
On page 131, the book says,
You can use parentheses to change how the terms of an expressions are grouped if the default grouping based on precedence and associativity is not what you want. The usage of parentheses for this purpose is fairly standard so I will not be discussing it any further.
If you like our products and services, please help us by posting your review here.

Username987654
Posts: 95
Joined: Sat Dec 26, 2015 6:37 pm
Contact:

Re: [HD Pg 129, Sec. 5.1.6 - precedence]

Post by Username987654 »

will be grouped as a = ( b = (c = 5) ;
The parenthesis aren't matching.

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: [HD Pg 129, Sec. 5.1.6 - precedence]

Post by admin »

Correct. Added to errata.

thank you for your feedback!
If you like our products and services, please help us by posting your review here.

enthunoob
Posts: 60
Joined: Thu Apr 15, 2021 12:21 pm
Contact:

Re: [HD Pg 129, Sec. 5.1.6 - precedence]

Post by enthunoob »

What could be a reason that (cast) has the (almost) highest precedence?

Post Reply

Who is online

Users browsing this forum: Google [Bot] and 26 guests