About Question enthuware.ocajp.i.v7.2.949 :
Moderator: admin
About Question enthuware.ocajp.i.v7.2.949 :
Hello,
In expression (b2 != b1 = !b2) first is evaluated !b2, but in "Explanation" section you said that first "b2 != b1" is evaluated.
The whole order of evaluation is:
1. !b2 -> true
2. b2 != b1 -> false
3. false = true -> compile time error
Please, correct me if I am wrong.
In expression (b2 != b1 = !b2) first is evaluated !b2, but in "Explanation" section you said that first "b2 != b1" is evaluated.
The whole order of evaluation is:
1. !b2 -> true
2. b2 != b1 -> false
3. false = true -> compile time error
Please, correct me if I am wrong.
Re: About Question enthuware.ocajp.i.v7.2.949 :
I don't know why according to http://docs.oracle.com/javase/tutorial/ ... ators.html "Operator Precedence":
unary operator "!" has more priority than equality operator "!=" ?
unary operator "!" has more priority than equality operator "!=" ?
Danail wrote:Hello,
In expression (b2 != b1 = !b2) first is evaluated !b2, but in "Explanation" section you said that first "b2 != b1" is evaluated.
The whole order of evaluation is:
1. !b2 -> true
2. b2 != b1 -> false
3. false = true -> compile time error
Please, correct me if I am wrong.
-
- Posts: 32
- Joined: Wed Mar 14, 2012 5:45 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.949 :
! has higher precedence than !=
-
- Posts: 5
- Joined: Wed May 01, 2013 5:46 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.949 :
When I took the test I answered "Compile time error", but got flagged for an incorrect answer. So, I copied the code from the test into a main method of a class I created.
I did indeed get a compiler error.
I think Enthuware needs to fix this question, unless they can prove me wrong.
The correct answer is: Compile time error
Code: Select all
class T2Q04_QID0949{
public static void main(String[] args){
boolean b1 = false;
boolean b2 = false;
if (b2 != b1 = !b2){
System.out.println("true");
}else{
System.out.println("false");
}
}
}
Code: Select all
T:\src\main\java>javac -d ..\..\..\target\classes T2Q04_QID0949.java
T2Q04_QID0949.java:10: error: unexpected type
if (b2 != b1 = !b2){
^
required: variable
found: value
1 error
T:\src\main\java>javac -version
javac 1.7.0_07
The correct answer is: Compile time error
-
- Site Admin
- Posts: 10066
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.949 :
I see that "Compile time error" is indeed set as the correct answer.
If you like our products and services, please help us by posting your review here.
-
- Posts: 22
- Joined: Wed Aug 21, 2013 12:29 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.949 :
Yes, I wanted to see what the forum had to say about this question. The explanation does not mention anything about how the ! unary operator has more precedence that the equality != or the assignment = operator.
So it seems like:
Regards,
javanaut
So it seems like:
- 1.is evaluated and returns true
Code: Select all
!b2
2.is evaluated which evaluates to true != false which returns trueCode: Select all
b2 != b1
3. Then the literal boolean value true is tried to be used like a variable container to take the value of the expressionbut it cannot do this as a literal is not a variable so the code fails and results in a compile time errorCode: Select all
!b2
Regards,
javanaut
-
- Site Admin
- Posts: 10066
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.949 :
Yes, unary operator has more precedence. But even with that logic, the answer will not be any different.
(b2 != b1 = !b2) =>
(b2 != b1 = true) => (!= has more precendence than =)
(false = true) Which is illegal
Remember that there will actually be no execution because of compilation failure. We are just trying to hypothetically execute the expression and see whether it boils down to an illegal situation or not. Here, we see that it does resolve to an illegal expression and so it will not compile.
HTH,
Paul.
(b2 != b1 = !b2) =>
(b2 != b1 = true) => (!= has more precendence than =)
(false = true) Which is illegal
Remember that there will actually be no execution because of compilation failure. We are just trying to hypothetically execute the expression and see whether it boils down to an illegal situation or not. Here, we see that it does resolve to an illegal expression and so it will not compile.
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 22
- Joined: Wed Aug 21, 2013 12:29 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.949 :
Cool.
And yes, I guess the compiler goes through this logic in nanoseconds to see that it will not compile. Thank-you for the reply Paul.
Regards,
Javanaut
And yes, I guess the compiler goes through this logic in nanoseconds to see that it will not compile. Thank-you for the reply Paul.
Regards,
Javanaut
-
- Posts: 4
- Joined: Sat Jan 20, 2024 9:26 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.949 :
Answer is correct, but explaination is a little incorrect in Enthuware:
"!b2 -> true" will be executed first,
and then "b2 != b1 -> false" will be executed,
resulting "false = true" in a Compile Time Error
"!b2 -> true" will be executed first,
and then "b2 != b1 -> false" will be executed,
resulting "false = true" in a Compile Time Error
Who is online
Users browsing this forum: No registered users and 150 guests