File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Assertion

 
srinivas sridaragaddi
Ranch Hand
Posts: 225
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



compilations fails at line 1

unreachable statement. please explain what does it mean
 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Howdy!

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.

Yours,
Bu.
[ October 13, 2007: Message edited by: Burkhard Hassel ]
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic