Answer . The top answer of this topic basically answers my question: Compiling a missing return statement in a non-void compiler .
I wonder why I do not need to return a value in this private method?
public class Test { private String testLoop() { while(true) { } } public static void main(String[] args) { Test test = new Test(); test.testLoop(); } }
I feel this should not compile. However, it compiles fine. Where is it defined as legal?
In this context, it seems strange to me that changing the method:
private String testLoop() { while(true) { if(false == true) { break; } } return null; }
requires me to provide a return type even if javap tells me that the compiler generates the same byte code for both testLoop implementations.
So, how and when does the Java compiler decide whether the method really requires a return value?
Unfortunately, the answer that mentioned the stop problem was deleted . I think that the Java compiler does not make any effort on trace methods, like the example above, since it cannot find all possible loops in the general setup.
java
Rafael winterhalter
source share