9

Answer: The top answer of this thread basically answers my question: Missing return statement in a non-void method compiles.


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 like this should not compile. However, it compiles fine. Where is this defined to be legal?

In this context, I find it weird that changing the method to:

private String testLoop() {
  while(true) { 
    if(false == true) { 
      break;
    }
  }
  return null;
}

requires me to provide a return type even though javap tells me that the compiler produces the exact same byte code for both implementations of testLoop.

So how and when does the Java compiler decide if a method actually needs a return value?


Unfortunately, an answer was deleted which mentioned the halting problem. I guess the Java compiler does not put effort on tracing methods like the example given above since it cannot find all possible loops in a general setting.

Community
  • 1
  • 1
Rafael Winterhalter
  • 42,759
  • 13
  • 108
  • 192
  • What do you deem to be illegal about it? – Mithrandir Nov 13 '13 at 11:34
  • Interestingly as soon as you put a `break;` inside the loop java gets upset again. The java compiler is more clever than I had imagined – Richard Tingle Nov 13 '13 at 11:38
  • 1
    The `if(false == true)` is explicitly excluded from the flow control check by the language specification to allow constructs like `if(DEBUG) …` where `DEBUG` is a `static final boolean`, i.e. a compile-time constant. So this `if(…) break;` statement is treated as “could happen or not” even if the condition expression is a compile-time constant. So you can change the value of the `DEBUG` constant without breaking the code. – Holger Dec 05 '13 at 15:41

5 Answers5

4
private String testLoop() {
    while(true) { }  //infinite loop
}

Above method never return since there is infinite loop there. So it is not expecting return Or it will never reach return.

Ruchira Gayan Ranaweera
  • 34,993
  • 17
  • 75
  • 115
1

your while loop while(true) { } will never end so compiler is not expecting for a return statement.If the compiler finds the loop will end then it wil expect for a return statement.You can manually test it.try with int i=1;while(i<0) this will give compile error and compiler will ask you to retrun a statement

SpringLearner
  • 13,738
  • 20
  • 78
  • 116
1

The method

private String testLoop() {
    while(true) { }
}

doesn't really have a return point during the compilation, which means that when it's compiled there never comes a time in which it "looks for" a return..

This isn't a good practice of course and empty cycles especially those with true condition should pop up as an error in your IDE.

Dropout
  • 13,653
  • 10
  • 56
  • 109
0

while(true) { } does not let to execute any further statement you write

private String testLoop() {
        while(true) { }
         //any code at this point is not reachable since it wont come out of while loop. So return statement just like any java code wont be reachable.
      }
NGoyal
  • 190
  • 2
  • 18
-4

your calling the testloop method, and not requiring a return value....if you wrote it like this, then the private method would have to return something.

String result = test.testloop();
DragonZero
  • 810
  • 5
  • 8