When does the condition “Unreachable Code” occur in Java?

后端 未结 3 1304
迷失自我
迷失自我 2021-01-19 01:53

When there is some statement written after the infinite loop, that statement becomes the unreachable code. For ex:

for(;;) 
{
}
Sytem.out.println(\"Test-1\")         


        
3条回答
  •  长发绾君心
    2021-01-19 02:20

    for(final int z=4;z<6;)
    {
    }
    System.out.println("Test-2"); //unreachable code --> z is final. Its value can't change. There is no way out of that loop
    
    
    
    final int z=4;
    for(;;)
    {
      if(z<2)  // z is 4  . The compiler only knows the value of "z". It doesn't know what z<2 evaluates to.. So, it still thinks taht there is a chance of getting out of that loop.
    break;
    }
    System.out.println("Test-3");  //So --> reachable code.
    

提交回复
热议问题