Java method with return type compiles without return statement

Question 1:

Why does the following code compile without having a return statement?

public int a() {
    while(true);
}

Notice: If I add return after the while then I get an Unreachable Code Error.

Question 2:

On the other hand, why does the following code compile,

public int a() {
    while(0 == 0);
}

even though the following does not.

public int a(int b) {
    while(b == b);
}

3 Answers
3

Leave a Comment