Page 1 of 1

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

Posted: Sun Jun 01, 2014 5:37 pm
by Dan Lee
I don't understand this,

boolean b1 = false;
int i1 = 2;
int i2 = 3;
if (b1 = i1 == i2)

My understanding,
i1 == i2 is false
b1 = i1 == i2 which is "if (false= false)" should be false

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

Posted: Sun Jun 01, 2014 7:36 pm
by admin
Why do you think false = false should compile?

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

Posted: Sun Jun 01, 2014 8:30 pm
by Dan Lee
Hm... I think should be,

if (b1 = false) which is false. Am I correct?

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

Posted: Sun Jun 01, 2014 9:03 pm
by admin
= is assignment operator and false is not a variable. Why do you think you can assign false to false?
"if (b1 = false)" is valid but that is not same as false = false.

if(b1 = false) implies you are assigning false to b1 and so the expression resolves to if(b1) i.e. if(false)

Hence, the complete expression b1 = i1 == i2 resolves like this:
b1 = (2 == 3)
=> b1 = false
=>b1
=>false

HTH,
Paul.

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

Posted: Wed Nov 26, 2014 1:25 am
by gparLondon
Here I would like to add a point. This question is based on operator precedence, == has higher precedence than =. Hence, 2==3 results into false, which is assigned to boolean variable, b1. so, if(false){}else{} here, else part is evaluated.

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

Posted: Thu Feb 05, 2015 3:54 pm
by Venceslas
I agree, I have failed to this question due to operator precedence. I have the feeling that the explanation should explain that JVM interpret the expression like if (b1 = (i1 == i2) )

By the way, is it required from us to know the precedence between the full range of the operators please?

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

Posted: Thu Feb 05, 2015 9:05 pm
by admin
It has now been added to the explanation.


Yes, you need to know operator precedence.

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

Posted: Sat Aug 08, 2015 1:16 pm
by NickWoodward
thought i'd quickly log in and just echo what the above posters have said.

explaining this question from the perspective of precendence makes it a simple answer, and to date it appears not to have been added (to my version at least!).

although i understand the answer, i don't think the current explanation is very good (sorry!).

fantastic piece of software though, it's a great help!

nick

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

Posted: Sat Aug 08, 2015 9:39 pm
by admin
I see that the explanation is included in question bank version 2.20.
The expression b1 = i1 == i2 will be evaluated as b1 = (i1 == i2) because == has higher precedence than =. Further, all an if statement needs is a boolean. Now i1 == i2 returns false which is a boolean and since b1 = false is an expression and every expression has a return value (which is actually the Left Hand Side of the expression), it returns false which is again a boolean. Therefore, in this case, the else condition will be executed.

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

Posted: Sun Jan 17, 2016 11:19 am
by TimothyTuckers
so, basically the first condition is:

if (false) {do this}. since the first condition is false, the program moves to the next step.

in this case the next step is else {do this}

is that a correct way of thinking about it??

thanks in advance.

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

Posted: Sun Jan 17, 2016 9:29 pm
by admin
Yes, that is correct.