简体   繁体   中英

Error In Java return type

I have this code:

public int Bol(int x, int y){
    if(x > y){
        return (x / y);
    }else if(x < y){
        return (y / x);
    }
}

Which throws this error: "This method must return a result of type int". Why must it?

I know that when I change else if to else this problem is solved. But why, when I use else-if, was I am getting this error?

What happens when none of the conditions are met (ie, when x == y )? What will the method return? So you need to return a default value:

public int Bol(int x, int y){
    if(x > y){
        return (x / y);
    }else if(x < y){
        return (y / x);
    }

    return 0; // or whatever default value you need
}

It would also be helpful to use a single return :

public int Bol(int x, int y){

    int result = 0; //whatever default value you want

    if(x > y) {
        result = (x / y);
    } else if(x < y) {
        result = (y / x);
    }

    return result;
}

"I know that when I change else if to else this problem is solved. But why, when I use else-if, was I getting this error?"

First, I suggest you go over how if , else , and else if work because understanding how branches work is core knowledge that you need to write code that functions correctly.

The reason it doesn't work when you use else if is because the code inside the else if block will only be executed if the boolean expression for the else if block evaluates to true. So the Java compiler sees that there is a case when none of the blocks get executed (so neither the if nor the else if block get executed). What that means is that there is a case where none of the return statements get executed. This violates the signature of the method which says that the method always returns an int . Now your code works when you change the else if to an else because the code inside an else will always run if the preceding if (or else if s) blocks haven't run.

So what happens when x == y? What do you return?

Your method was defined to return an int. But it wasn't returning anything when x == y.

You could have written

if (x > y) {
    return x/y;
} else if (x < y) {
    return y/x;
} else {
    return 1;  // x == y therefore x/y is 1.
}

Also, watch out for when x or y is zeor. Say x=1 and y=0. What do you think would happen?

there is a case where your code does not return a value. IE if neither if or else if conditions are satisfied. Hence, the compiler flags an error.

If you have a else instead of else if , the compiler is sure in both cases you return a value. That's why it is fine.

Also note - it is not enough that "all cases are covered". For example, if you change the else if to

else if ( x <= y ) {
....
}

Then technically all cases are covered ( except concurrent modifications ). Still, it is not easy for the compiler to know that and it will flag an error.

The only way the compiler is sure that you have a return value is a else or a return outside the if condition .

Right now there is a possibility that your function does not return anything. For example, if x = y, then neither (x > y) nor (x < y) is true. Because your function only has return statements defined when x > y and when y > x, it will not return under all cases.

One way to fix this is to add a default return statement after your if statements:

public int Bol(int x, int y){
    if(x > y){
        return (x / y);
    }else if(x < y){
        return (y / x);
    }
    return 1;
}

What none of the other responders have noted at this point is that your method signature is requiring a return type:

public int Bol(int x, int y){

The int after public indicates the return type. You must return something of that type from this method because Java is a strongly typed language . When you use else in an if it will catch all cases not caught by the other conditionals.

public int Bol(int x, int y){
    if(x > y){
        return (x / y);//returns a double, which can be cast to an int
    }else if(x < y){
        return (y / x); //returns a double, which can be cast to an int
    }
}    //if this line is reached, nothing is being returned, so the compiler throws an error.

Understand your code logic. Can there be a case wherein both if conditions are not satisfied? Yes, that is possible (ie when x==y ) and in that situation the function does not return anything. For that very condition, you need to have the default return value in the function, you have missed that and the compiler is complaining.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM