Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Java recognize infinite loops?

Given the following code sample:

public class WeirdStuff {      public static int doSomething() {         while(true);     }      public static void main(String[] args) {         doSomething();     } } 

This is a valid Java program, although the method doSomething() should return an int but never does. If you run it, it will end in an infinite loop. If you put the argument of the while loop in a separate variable (e.g. boolean bool = true) the compiler will tell you to return an int in this method.

So my question is: is this somewhere in the Java specification and are there situation where this behavior might be useful?

like image 286
leifg Avatar asked Dec 24 '09 14:12

leifg


People also ask

Is infinite loop possible in java?

In this quick tutorial, we'll explore ways to create an infinite loop in Java. Simply put, an infinite loop is an instruction sequence that loops endlessly when a terminating condition isn't met. Creating an infinite loop might be a programming error, but may also be intentional based on the application behavior.

Is it possible to detect infinite loop?

While most infinite loops can be found by close inspection of the code, there is no general method to determine whether a given program will ever halt or will run forever; this is the undecidability of the halting problem.

How do you handle an infinite loop in java?

Do consider getting rid of the variables loop and num and instead use while (true) { try { ....; return reader. nextInt(); } catch {....} }

What is known as infinite loop in java?

What is an Infinite Loop? An infinite loop occurs when a condition always evaluates to true. Usually, this is an error. For example, you might have a loop that decrements until it reaches 0. public void sillyLoop( int i ) { while ( i != 0 ) { i-- ; } }


2 Answers

I'll just quote the Java Language Specification, as it's rather clear on this:

This section is devoted to a precise explanation of the word "reachable." The idea is that there must be some possible execution path from the beginning of the constructor, method, instance initializer or static initializer that contains the statement to the statement itself. The analysis takes into account the structure of statements. Except for the special treatment of while, do, and for statements whose condition expression has the constant value true, the values of expressions are not taken into account in the flow analysis.

...

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.

...

Every other statement S in a nonempty block that is not a switch block is reachable iff the statement preceding S can complete normally.

And then apply the above definitions to this:

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. Here is one example:

class DizzyDean {   int pitch() { throw new RuntimeException("90 mph?!"); } } 
like image 81
Pavel Minaev Avatar answered Sep 23 '22 21:09

Pavel Minaev


Java specification defines a concept called Unreachable statements. You are not allowed to have an unreachable statement in your code (it's a compile time error). A while(true); statement makes the following statements unreachable by definition. You are not even allowed to have a return statement after the while(true); statement in Java. Note that while Halting problem is undecidable in generic case, the definition of Unreachable Statement is more strict than just halting. It's deciding very specific cases where a program definitely does not halt. The compiler is theoretically not able to detect all infinite loops and unreachable statements but it has to detect specific cases defined in the spec.

like image 33
mmx Avatar answered Sep 23 '22 21:09

mmx