Page 1 of 1
About Question enthuware.ocajp.i.v7.2.949 :
Posted: Tue Feb 26, 2013 2:32 am
by Danail
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.
Re: About Question enthuware.ocajp.i.v7.2.949 :
Posted: Tue Feb 26, 2013 2:42 am
by Danail
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 "!=" ?
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.
Re: About Question enthuware.ocajp.i.v7.2.949 :
Posted: Wed Mar 13, 2013 12:18 pm
by baptize
! has higher precedence than !=
Re: About Question enthuware.ocajp.i.v7.2.949 :
Posted: Sun Jun 30, 2013 8:28 pm
by openmic62
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.
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");
}
}
}
I did indeed get a compiler error.
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
I think Enthuware needs to fix this question, unless they can prove me wrong.
The correct answer is:
Compile time error
Re: About Question enthuware.ocajp.i.v7.2.949 :
Posted: Sun Jun 30, 2013 8:37 pm
by admin
I see that "Compile time error" is indeed set as the correct answer.
Re: About Question enthuware.ocajp.i.v7.2.949 :
Posted: Fri Nov 08, 2013 4:14 pm
by javanaut
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:
- 1. is evaluated and returns true
2. is evaluated which evaluates to true != false which returns true
3. Then the literal boolean value true is tried to be used like a variable container to take the value of the expression but it cannot do this as a literal is not a variable so the code fails and results in a compile time error
What does the enthuware hive mind think?
Regards,
javanaut
Re: About Question enthuware.ocajp.i.v7.2.949 :
Posted: Fri Nov 08, 2013 4:59 pm
by admin
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.
Re: About Question enthuware.ocajp.i.v7.2.949 :
Posted: Fri Nov 08, 2013 8:16 pm
by javanaut
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
Re: About Question enthuware.ocajp.i.v7.2.949 :
Posted: Thu Jan 25, 2024 1:41 am
by agupta108
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