As far as I know Javac or any other compiler never try to find program is in infinite loop or not. because one can not write a program to find a program is in infinite loop with complete accuracy.
Then how they identify unreachable statement? see the code below... both the code are in infinite loop
class foo
{
public static void main(String[]
(
Read more... )
Comments 7
It is very easy for compiler to detect that while(true) is an infinite loop and so it has to be supplemented by a break in the block for it to exit. But it is very difficult for compiler to predict that the value of j will not change at any point of time and so it does not give ant error.
Reply
Reply
Reply
class Test{
static void foo(){
while(true){
if(false)
break;
}
return;
}
public static void main(String[] args){
foo();
}
}
Reply
In the first case, at the compilation time, the compiler knows that the loop needs to be executed infinitely as the terminating condition for the while loop is always true.
while(j==0)
In the second case, at the compilation time, the compiler does not know whether the loop will be executed infinitely as the terminating condition is dependent on the value of the variable j.
More details can be obtained from the Java Language Specifications at this URL.
Reply
class foo
{
public static void main(String[] args)
{
boolean f = true;
while(f)
System.out.println("Hello");
return;
}
}
Reply
"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."
Reply
Leave a comment