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

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
elengican
Posts: 1
Joined: Thu Mar 31, 2016 12:55 pm
Contact:

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

Post 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?

admin
Site Admin
Posts: 10385
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.

DazedTurtle
Posts: 26
Joined: Wed Oct 02, 2019 1:42 pm
Contact:

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

Post 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.

admin
Site Admin
Posts: 10385
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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

Post Reply

Who is online

Users browsing this forum: No registered users and 11 guests