Networking Space
Networking Space

Reputation: 11

Missing return statement Challenge.java:7

I am running this java code and I am getting an error

/Challenge.java:7: error: missing return statement

public class Challenge {

    public static boolean isEqual(int num1, int num2) {
        if( num1 == num2 )
            return true;    
    }

    public static void main(String args[]){
        System.out.println( isEqual( 8, 8 ));
    }
}

Upvotes: 1

Views: 43

Answers (2)

Marwen Jaffel
Marwen Jaffel

Reputation: 807

You should add return statement if num1 != num2 like this

public static boolean isEqual(int num1, int num2) {

if( num1 == num2 )
    {return true;}
return false;

}

Upvotes: 1

Mureinik
Mureinik

Reputation: 311163

You need a return statement in every branch of the method. In the isEqual method, if the if's condition isn't met, you don't have a return statement. You could add one:

public static boolean isEqual(int num1, int num2) {
    if ( num1 == num2 )
        return true;
    return false; // Here!    
}

Or, even simpler, you could return the == expression directly and avoid the problem altogether:

public static boolean isEqual(int num1, int num2) {
    return num1 == num2;
}

Upvotes: 1

Related Questions