About Question enthuware.ocajp.i.v7.2.1156 :

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

Moderator: admin

Post Reply
ETS User

About Question enthuware.ocajp.i.v7.2.1156 :

Post by ETS User »

I assumed because "&&" had higher precedence then "||", then "(b=true) && (c=true)" would have been evaluated first, resulting in "true, true, true" for an answer. Could you tell me why the "&&" isn't evaluated first ... I must be misunderstanding or missing something here.

ETS user

Re: About Question enthuware.ocajp.i.v7.2.1156 :

Post by ETS user »

Thank you ... geesh, I missed that point. I appreciate your very clear explanation.

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

Re: About Question enthuware.ocajp.i.v7.2.1156 :

Post by admin »

Very sorry, I was not 100% sure about what I wrote earlier so I deleted it in a minute or so but it seems you had already read it by then. Since you are not a registered user, I have no way to notifying you about it. Hopefully you will see this message.


So anyway, the observed behavior is because of the evaluation order of the expression coupled with the short circuiting nature of || rather than precedence. && does have higher precedence. However, evaluation order of an expression is left to right. So the expression is indeed grouped as:

Code: Select all

(a = true) || ((b = true) && (c = true))
in the parse tree.
However, the expression is evaluated from left to right. Since a=true results in true and || is a short circuit operator, there is no need to evaluate the right branch of the three.

It is similar to a + b * c. It is grouped as a + (b*c). Although, in this case + and * are not short circuit operators so the right branch (b*c) has to be evaluated but otherwise the grouping is exactly the same.

HTH,
Paul.
If you like our products and services, please help us by posting your review here.

ETS user

Re: About Question enthuware.ocajp.i.v7.2.1156 :

Post by ETS user »

I appreciate the correction ... that makes more sense to me. Your original answer was a bit worrisome because it threw me for a loop. This makes more sense.

nickeasyuptech
Posts: 29
Joined: Sat Jun 08, 2013 11:33 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1156 :

Post by nickeasyuptech »

Hi Paul,

so it is

Code: Select all

(a = true) || ((b = true) && (c = true))
as opposed to

Code: Select all

((a = true) || (b = true)) && (c = true)
because && has higher precedence?

Thanks, Nick

insider
Posts: 29
Joined: Wed Apr 17, 2013 9:22 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1156 :

Post by insider »

I expected that expressions enclosed in brackets get evaluated first leading us to three assignments prior to any other logical operator.

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

Re: About Question enthuware.ocajp.i.v7.2.1156 :

Post by admin »

The question arises because there are no brackets in (a = true) || (b = true) && (c = true) that can determine the grouping.

HTH,
Paul.
If you like our products and services, please help us by posting your review here.

shining_dragon
Posts: 14
Joined: Sat Mar 01, 2014 9:12 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1156 :

Post by shining_dragon »

how about in this example:
true || false && false >> although the answer in this expression is true. I just want to know how JVM evaluates this expression.

will java execute (false && false) first then true in the left hand operand of OR

or


java will execute true in the left hand operand of OR then will not evaluate (false && false)

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

Re: About Question enthuware.ocajp.i.v7.2.1156 :

Post by admin »

If you really want to understand the whole story, you should go through this: http://docs.oracle.com/javase/specs/jls ... ls-15.html

HTH,
Paul.
If you like our products and services, please help us by posting your review here.

vchhang
Posts: 36
Joined: Tue May 06, 2014 8:30 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1156 :

Post by vchhang »

I don't understand why the && wasn't evaluated first since it had higher precedence. According to the link you provided "15.7.3. Evaluation Respects Parentheses and Precedence", the && should have been evaluated first. The first expression to the left of the && should have been evaluated first.

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

Re: About Question enthuware.ocajp.i.v7.2.1156 :

Post by admin »

vchhang wrote:I don't understand why the && wasn't evaluated first since it had higher precedence. According to the link you provided "15.7.3. Evaluation Respects Parentheses and Precedence", the && should have been evaluated first. The first expression to the left of the && should have been evaluated first.
There is not just one rule of precedence but multiple rules that are at play here. As per 15.7.1,
"The Java programming language guarantees that the operands of operators appear to be evaluated in a specific evaluation order, namely, from left to right.".
The first operator that the parser encounters is ||. Therefore, it must first evaluate the left side of it, which is a= true. Then whole of the right side of || becomes the second operand. Thus, the given expression will be grouped as (a = true) || ((b = true) && (c = true)). Since || is a short circuit operator and since the left side is true already, there is no need to evaluate the right side.
If you like our products and services, please help us by posting your review here.

aleksjej
Posts: 2
Joined: Mon Jun 16, 2014 5:04 pm
Location: Gdansk/PL
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1156 :

Post by aleksjej »

Thanks for the answers. I do understand that short circuit takes place here, but I don't understand why the operator && has higher precedence than || then... (they should have the same in this case for me). Can you give some example showing higher precedence of the && operator?

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

Re: About Question enthuware.ocajp.i.v7.2.1156 :

Post by admin »

They do have the same precedence. Please read the post above.
If you like our products and services, please help us by posting your review here.

voltore
Posts: 1
Joined: Thu Sep 11, 2014 3:54 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1156 :

Post by voltore »

tricksy hobbitses

aditya
Posts: 3
Joined: Sun Jul 27, 2014 6:20 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1156 :

Post by aditya »

I also dont understand Please explain in detail.

NickWoodward
Posts: 29
Joined: Mon Mar 30, 2015 6:00 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1156 :

Post by NickWoodward »

precendence != evaluation order.

it is true that && has the higher precedence, so you place the brackets around (b && c)

HOWEVER

evaluation order trumps precendence, so it is evaluated as

1. -----> a ||
2. -----> (b&&c)

with the short circuit || meaning that 2 is never evaluated

edward109
Posts: 3
Joined: Sun Feb 05, 2017 11:39 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1156 :

Post by edward109 »

1. bool = ((a = true) || (b = true)) && (c = true) evaluates to
2. bool = true || false && false ;

bool evaluates to true. (tested)
This means in line 1 no precedence of && (from left to right evaluation)
but in line 2 precedence of && DOES take place : hence bool evaluates to true.
Why does precedence apply in line 2 but not in line 1 ?
Not consequent or am I missing something ?

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

Re: About Question enthuware.ocajp.i.v7.2.1156 :

Post by admin »

I am sorry but I am not able to understand your question but here is a good discussion of precedence and ordering that might help you: http://stackoverflow.com/questions/6800 ... er-in-java
If you like our products and services, please help us by posting your review here.

Parth Vishvajit
Posts: 1
Joined: Sun Mar 26, 2017 1:42 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1156 :

Post by Parth Vishvajit »

If we assume that the grouping concept is true as per discussion,
What the below code should print according to that grouping concept?

boolean bool = (a = false) && (b = true) || (c = true);
System.out.println(a+" "+b+" "+c);

--> false false false , right?
As the expression should be like, (a = false) && ((b = true) || (c = true)) , and so the left hand operand is false and the && is short circuit type operator, right hand expression supposed to be ignored.
But I got the output as
--> false false true
And expression also evaluates to true. (printing bool variable also gives true)
---> So, I think grouping concept is not satisfying here that what actually going on behind the scene. At least not with what we have thought.
--> There is also a question about priority that, () parentheses has the higher priority among all of these , then why it's not assigning true to every boolean variable first ?? Also grouping concept does not sound proper as it is not the natural evaluation, what is the importance of programmer's freedom of writing any logic, if compiler implicitly change the priority of operation in the expression [It actually means it is changing the expression, which it should not] ??

Correct me if I am missing something or understanding something incorrectly,
Thank you.

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

Re: About Question enthuware.ocajp.i.v7.2.1156 :

Post by admin »

The only way this can be answered is that && has more precedence than ||. Although I could not find it explicitly stated anywhere in the specification.
-Paul.
If you like our products and services, please help us by posting your review here.

wardeinstein
Posts: 1
Joined: Tue Mar 07, 2017 8:01 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1156 :

Post by wardeinstein »

just check this one
https://docs.oracle.com/javase/tutorial ... ators.html

It shows && has higher precedence than ||

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

Re: About Question enthuware.ocajp.i.v7.2.1156 :

Post by flex567 »

The response from admin was that both operators have the same precedence which is not true.
They do have the same precedence. Please read the post above.
Can you agree that they don't have the same precedence?

You can see that on this page as well:
http://www.cs.bilkent.edu.tr/~guvenir/c ... dence.html

Are we allowed to have the precedence table on the Exam?

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

Re: About Question enthuware.ocajp.i.v7.2.1156 :

Post by admin »

please go throught the complete discussion above.

>Are we allowed to have the precedence table on the Exam?
No.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 56 guests