Page 1 of 1

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

Posted: Mon Apr 25, 2016 9:14 am
by elengican
"Because the expression 'bool = false' returns a boolean ( which happens to be false)"
Is the above explanation correct for option number 4 for this question. Or, it does not have a compile and run time error because, after the assignment "bool=false" it gets evaluated to "if(bool)" and the value of bool is false at that point?

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

Posted: Mon Apr 25, 2016 9:47 am
by admin
The explanation is correct. In Java, every assignment expression has a value, which is same as the value of the variable after the assignment has occurred. Therefore, the expression "bool = false" itself also has a value which happens to be same as the left hand side of the assignment i.e. the value of bool after assignment.

HTH,
Paul.

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

Posted: Fri Oct 25, 2019 1:48 pm
by DazedTurtle
Are there other languages that would allow "if(x = 3)" to compile? Because I remember I once spent hours trying to figure out what was wrong with my code, and it ended up being a single equals sign inside an if statement.

I'm pretty sure it compiled, because if it hadn't, I would have seen the line number that caused the issue, and it wouldn't have taken hours.

And I'm pretty sure it wasn't a boolean because there's really no reason I would have attempted "bool == false" rather than just "!bool".

I just want to know if I'm misremembering the issue, or if it's entirely possible this was a problem and I'm just misremembering what language I was coding in at the time.

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

Posted: Fri Oct 25, 2019 10:42 pm
by admin
Any language that does not have a boolean data type will have this problem. For example, c uses int for a boolean (0 is false and any other value is true).
See this: https://stackoverflow.com/questions/146 ... nt-integer