Andrew the Programmer Andrew the Programmer - 1 month ago 8
Java Question

Why am I missing a return statement with else

I'm trying to understand why my compiler is complaining that I'm missing a return statement, when in fact I have fail-safe else at the end of my method. Why is this the case? When would the else not cover a return?

Here's a primitive example:

public boolean greaterOrEqual(int a){
int z = 10;
if(z > a){
return false;
}
else if(z < a){
return true;
}
else{
return true; //if if and else if fail, else is the last resort
}
}


Functionally, it would be the same as this

public boolean greaterOrEqual(int a){
int z = 10;
if(z > a){
return false;
}
else if(z < a){
return true;
}

return true; //if if and else if fail, returns true

}

Answer

I believe the following will solve your problem:

public boolean greaterOrEqual(int a){
    int z = 10;
    return z <= a;
}

and by the way, there are many interesting articles on StackOverflow help center. I would recommend How to create a Minimal, Complete, and Verifiable example and How to ask.

The problem you have described cannot be reproduced. Each of us can compile it.