About Question enthuware.ocajp.i.v7.2.1070 :
Moderator: admin
-
- Posts: 4
- Joined: Tue May 20, 2014 12:41 am
- Contact:
About Question enthuware.ocajp.i.v7.2.1070 :
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
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
-
- Site Admin
- Posts: 10053
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1070 :
Why do you think false = false should compile?
If you like our products and services, please help us by posting your review here.
-
- Posts: 4
- Joined: Tue May 20, 2014 12:41 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1070 :
Hm... I think should be,
if (b1 = false) which is false. Am I correct?
if (b1 = false) which is false. Am I correct?
-
- Site Admin
- Posts: 10053
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1070 :
= 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.
"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.
If you like our products and services, please help us by posting your review here.
-
- Posts: 63
- Joined: Fri Oct 31, 2014 6:31 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1070 :
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.
-
- Posts: 15
- Joined: Thu Feb 05, 2015 3:50 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1070 :
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?
By the way, is it required from us to know the precedence between the full range of the operators please?
-
- Site Admin
- Posts: 10053
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1070 :
It has now been added to the explanation.
Yes, you need to know operator precedence.
Yes, you need to know operator precedence.
If you like our products and services, please help us by posting your review here.
-
- Posts: 29
- Joined: Mon Mar 30, 2015 6:00 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1070 :
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
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
-
- Site Admin
- Posts: 10053
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1070 :
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.
If you like our products and services, please help us by posting your review here.
-
- Posts: 2
- Joined: Fri Jan 08, 2016 3:06 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1070 :
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.
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.
-
- Site Admin
- Posts: 10053
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1070 :
Yes, that is correct.
If you like our products and services, please help us by posting your review here.
Who is online
Users browsing this forum: Google [Bot] and 43 guests