This week's book giveaway is in the OCMJEA forum. We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line! See this thread for details.
Remember, that using assertions depends on wheter you enable them or not when you start the application. For example, when you use your main2 class (with line 1 //commented out it compiles nicely) like
When you start this user class, with >java UserOfm2
then the assertions are by default disabled, and you just get the sentence from the main-method.
but when you use >java -ea UserOfm2 an assertion error will be called from the foo() method and the program stops after the first assertion of false.
Because of the fact that the compiler does not know if later the program will be called with assertions enabled or disabled, two assertions of false one after the other cannot be a compile time error due to unreacheable code.
The situation below in your original main2 class is different:
This produces a compile time error due to "unreacheable code". But here, the fact that line 1 is also an assertion is not so important. Any code at the place of line 1 would cause this compile time error. Because in while(true)...
true is a compile time constant, a boolean literal, and the compiler knows for sure, that this loop (without any break in it) can never been left. Therefore anything that is "behind" this loop is unreachable code that produces a compile time error that forces the programmer to fix this bug.
And again the code inside the loop does not necessarily cause the loop to be left because again you could start your application with the assertions disabled.