Hello,
I have a question about the meaning (evaluation) of Boolean variables in return statements in Java.
I know that:
if (var) { ... }
is the same as:
if (var==true) { ... }
In the second case we explicitly say var==true, but we don't need to do this, because Java evaluates var as true anyway. I hope I have understood this right.
My question is: is it the same when Boolean variables are returned? When we have a return statement?
For example, a task specifies: the method looksBetter() will return true only if b < a. My solution was:
public boolean looksBetter() {
if (b < a) {
return true;
} else {
return false;
}
}
The simple answer was:
public boolean lookBetter() {
return b < a;
}
So, it seems that here we have again this implicit assumption that in case b < a == true, the return of the method is true. I am sorry ... it seems very trivial, but I am somehow not comfortable with this, and I don't know why. Thank you.