About Question enthuware.ocajp.i.v7.2.1156 :
Moderator: admin
About Question enthuware.ocajp.i.v7.2.1156 :
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 :
Thank you ... geesh, I missed that point. I appreciate your very clear explanation.
-
- Site Admin
- Posts: 10090
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1156 :
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: 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.
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))
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.
Re: About Question enthuware.ocajp.i.v7.2.1156 :
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.
-
- Posts: 29
- Joined: Sat Jun 08, 2013 11:33 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1156 :
Hi Paul,
so it is
as opposed to
because && has higher precedence?
Thanks, Nick
so it is
Code: Select all
(a = true) || ((b = true) && (c = true))
Code: Select all
((a = true) || (b = true)) && (c = true)
Thanks, Nick
-
- Posts: 29
- Joined: Wed Apr 17, 2013 9:22 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1156 :
I expected that expressions enclosed in brackets get evaluated first leading us to three assignments prior to any other logical operator.
-
- Site Admin
- Posts: 10090
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1156 :
The question arises because there are no brackets in (a = true) || (b = true) && (c = true) that can determine the grouping.
HTH,
Paul.
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 14
- Joined: Sat Mar 01, 2014 9:12 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1156 :
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)
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)
-
- Site Admin
- Posts: 10090
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1156 :
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.
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 36
- Joined: Tue May 06, 2014 8:30 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1156 :
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.
-
- Site Admin
- Posts: 10090
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1156 :
There is not just one rule of precedence but multiple rules that are at play here. As per 15.7.1,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.
"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.
-
- Posts: 2
- Joined: Mon Jun 16, 2014 5:04 pm
- Location: Gdansk/PL
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1156 :
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?
-
- Site Admin
- Posts: 10090
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1156 :
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.
-
- Posts: 1
- Joined: Thu Sep 11, 2014 3:54 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1156 :
tricksy hobbitses
-
- Posts: 3
- Joined: Sun Jul 27, 2014 6:20 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1156 :
I also dont understand Please explain in detail.
-
- Posts: 29
- Joined: Mon Mar 30, 2015 6:00 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1156 :
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
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
-
- Posts: 3
- Joined: Sun Feb 05, 2017 11:39 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1156 :
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 ?
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 ?
-
- Site Admin
- Posts: 10090
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1156 :
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.
-
- Posts: 1
- Joined: Sun Mar 26, 2017 1:42 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1156 :
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.
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.
-
- Site Admin
- Posts: 10090
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1156 :
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.
-Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 1
- Joined: Tue Mar 07, 2017 8:01 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1156 :
just check this one
https://docs.oracle.com/javase/tutorial ... ators.html
It shows && has higher precedence than ||
https://docs.oracle.com/javase/tutorial ... ators.html
It shows && has higher precedence than ||
-
- Posts: 202
- Joined: Mon Apr 02, 2018 8:40 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1156 :
The response from admin was that both operators have the same precedence which is not true.
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?
Can you agree that they don't have the same precedence?They do have the same precedence. Please read the post above.
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?
-
- Site Admin
- Posts: 10090
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1156 :
please go throught the complete discussion above.
>Are we allowed to have the precedence table on the Exam?
No.
>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.
Who is online
Users browsing this forum: No registered users and 23 guests