Docker Layer Caching
Docker Layer Caching
May 23, 2023
Quality Risks in Agile Project
Quality Risks in Agile Project
June 6, 2023

May 30, 2023

How to fix the compile error?

Why is a compiler error received for the following code in Java?

public static int compileError(int n) {
    if (n > 0)
       return 10;
    else if (n == 0)
       return 0;
    else if (n < 0)
        return -10;
}

Since there is no "else" statement, the compiler cannot be sure that the method always returns an integer because if conditions might not cover all the statements.  The compiler error can be fixed by changing the last "else if" with "else".

public static int noCompileError(int n) {
    if (n > 0)
       return 10;
    else if (n == 0)
       return 0;
    else
        return -10;
}
How to fix the compile error?
This website uses cookies to improve your experience. By using this website you agree to our Data Privacy Statement
Read more