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:
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").