Page 1 of 2

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

Posted: Sat Dec 15, 2012 8:15 pm
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.

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

Posted: Sat Dec 15, 2012 9:01 pm
by ETS user
Thank you ... geesh, I missed that point. I appreciate your very clear explanation.

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

Posted: Sat Dec 15, 2012 9:22 pm
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.

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

Posted: Sun Dec 16, 2012 11:28 am
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.

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

Posted: Mon Jun 17, 2013 5:14 pm
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

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

Posted: Fri Sep 06, 2013 1:44 am
by insider
I expected that expressions enclosed in brackets get evaluated first leading us to three assignments prior to any other logical operator.

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

Posted: Fri Sep 06, 2013 6:08 am
by admin
The question arises because there are no brackets in (a = true) || (b = true) && (c = true) that can determine the grouping.

HTH,
Paul.

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

Posted: Sat Mar 15, 2014 8:43 am
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)

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

Posted: Sat Mar 15, 2014 9:15 am
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.

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

Posted: Thu May 22, 2014 10:06 am
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.

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

Posted: Thu May 22, 2014 12:06 pm
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.

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

Posted: Mon Jun 16, 2014 5:07 pm
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?

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

Posted: Mon Jun 16, 2014 7:28 pm
by admin
They do have the same precedence. Please read the post above.

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

Posted: Sun Sep 14, 2014 3:42 pm
by voltore
tricksy hobbitses

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

Posted: Tue Oct 14, 2014 2:23 am
by aditya
I also dont understand Please explain in detail.

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

Posted: Tue Nov 17, 2015 11:38 am
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

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

Posted: Mon Feb 13, 2017 8:57 am
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 ?

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

Posted: Mon Feb 13, 2017 9:31 pm
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

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

Posted: Sun Mar 26, 2017 1:56 pm
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.

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

Posted: Mon Mar 27, 2017 12:54 am
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.

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

Posted: Tue Jul 25, 2017 11:04 am
by wardeinstein
just check this one
https://docs.oracle.com/javase/tutorial ... ators.html

It shows && has higher precedence than ||

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

Posted: Sun May 05, 2019 12:42 pm
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?

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

Posted: Sun May 05, 2019 6:23 pm
by admin
please go throught the complete discussion above.

>Are we allowed to have the precedence table on the Exam?
No.

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

Posted: Sat Aug 10, 2024 10:40 am
by raphaelzintec
yo this OCA oracle is frustrating

i feel really frustrated because i've read PDF 20 times and i keep doing errors

boolean bool = (a = true) || (b = true) && (c = true);

() has the highest priority so first compiler must affect true to a,b,c
then && has higher priority

but indeed when i run code on intellij i get true, false, false

it's a non sens

why compiler doesn't evaluate () first?????????????????????

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

Posted: Sun Aug 11, 2024 2:44 am
by admin
Evaluation always happens from left to right.
Parentheses change the grouping of operation and not the evaluation order.
You can go through Section 10.1.6 of Deshmukh's OCP book. It explains this quite clearly:
test.png
test.png (151.04 KiB) Viewed 6535 times