If I have a method with an infinite loop, is it not necessary for the method to return what its signature says?
Above code compiles just fine even though the method has no return statement. Logically it makes sense because a return after the loop would be unreachable. But I feel that even the above code should not compile as the method says it returns 'int' but actually does not.
Originally posted by Satya Maheshwari: Logically it makes sense because a return after the loop would be unreachable. But I feel that even the above code should not compile as the method says it returns 'int' but actually does not.
Well, the Java compiler doesn't care how you feel...
The compiler also checks for unreachable code, and as you say yourself, a return statement after the infinite loop would be unreachable code. It would be strange if the compiler would first say "you must add a return statement" and then "the return statement is unreachable". That would make the code, which in principle works without problems, impossible to compile.
Maybe another example which has similar oddity maybe help you
In the viewpoint of method main(), it only knows that getValue() will return int value. It doesn't know that getValue() will *always* throw RuntimeException. [ September 02, 2008: Message edited by: Bupjae Lee ]
If a method is declared to have a return type, then every return statement (�14.17) in its body must have an Expression. A compile-time error occurs if the body of the method can complete normally (�14.1).
In other words, a method with a return type must return only by using a return statement that provides a value return; it is not allowed to "drop off the end of its body."
Note that it is possible for a method to have a declared return type and yet contain no return statements.
A while statement can complete normally iff at least one of the following is true:
* The while statement is reachable and the condition expression is not a constant expression with value true. * There is a reachable break statement that exits the while statement.
Condition expression (true) is constant expression with value true and there is no break statement. So the while statement and the method body cannot complete normally.
[ September 02, 2008: Message edited by: Vlado Zajac ] [ September 02, 2008: Message edited by: Vlado Zajac ]
Joined: Jan 01, 2007
Originally posted by Campbell Ritchie: Try changing while (true) to while (3 > 2) and see what difference that makes.
Here 'return' is mandatory even though its unreachable.
That's because the value "true" is a compile-time constant, which means that the compiler can check for it, so that it knows that it's an infinite loop. The expression "3 > 2" is evaluated at runtime (not by the compiler at compile-time), so the compiler doesn't know in advance that the result of the expression is always true.
Jesper Young ---------------------------------------------------------------------------- The expression "3 > 2" is evaluated at runtime (not by the compiler at compile-time), --------------------
I'm just checking my understanding about this. Any expression will be evaluated at runtime or one with constants will evaluate at compile time ? Since expression 3 > 2 contains both constants isn't it evaluated at compile time? If 3 > 2 evaluated at runtime then compiler should have complains about it. May be Jesper Young means a > b.
Joined: Apr 14, 2008
Sorry i meant while(a>b) needs a return statement not 3>2.
But the thing that comes out here is that true is a constant and while evaluating with 3>2 we are dealing with constants again so the compiler knows at compile time that any code beyond this will be unreachable.
In case of a>b the value of a and b can be changed inside the while loop or somewhere in our code and then the result might be false so the compiler checks for return statement.