"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?
About Question enthuware.ocajp.i.v7.2.1269 :
Moderator: admin
-
- Posts: 1
- Joined: Thu Mar 31, 2016 12:55 pm
- Contact:
-
- Site Admin
- Posts: 10385
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1269 :
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.
HTH,
Paul.
-
- Posts: 26
- Joined: Wed Oct 02, 2019 1:42 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1269 :
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.
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.
-
- Site Admin
- Posts: 10385
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1269 :
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
See this: https://stackoverflow.com/questions/146 ... nt-integer
Who is online
Users browsing this forum: No registered users and 11 guests