while(true); loop throws Unreachable code when isn't in a void

后端 未结 4 820
时光说笑
时光说笑 2020-12-29 20:28

I was doing some small programs in java. I know that if I write while(true); the program will freeze in this loop. If the code is like that:

4条回答
  •  遥遥无期
    2020-12-29 20:50

    The language spec has an exact definition what the compiler should treat as unreachable code, see also https://stackoverflow.com/a/20922409/14955.

    In particular, it does not care about if a method completes, and it does not look inside other methods.

    It won't do more than that.

    However, you could get static code analysis tools like FindBugs to get a "deeper" analysis (not sure if they detect the pattern you described, though, either, and, as has been pointed out by others, the halting problem in all generality cannot be algorithmically solved anyway, so one has to draw the line at some reasonably definition of "best effort").

提交回复
热议问题