Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Unreachable code

 
Jason Attin
Ranch Hand
Posts: 232
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This question is really interesting, I have absolutely no idea why some of the statement don't compile. Let's have a look at it, as it seems to test unreachable code.

Which of the following code snippets will compile without any errors? (Assume that the statement int x = 0; exists prior to the statements below.)
A.
B.
C.
D.

I wasn't sure why one would compile and the other didn't, to me they all look absolutely fine. But, apparently, A doesn't compile because x=3 is not reachable. Well, it's not reachable in B and D too but apparently those are OK. It's the first time I've heard that some code doesn't compile because the condition of a loop is false...Can somebody shed some light on it as I have absolutely no idea whatsoever please?
thanks

 
Jason Attin
Ranch Hand
Posts: 232
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah small correction. Apparently D doesn't compile either because x=3 is unreachable there too but it's allowed, here is the explanation, which still makes no sense to me.
while is a compile-time error because the statement is not reachable; Similarly, is also a compile time error because is unreachable. In , although the body of the condition is unreachable, this is not an error because the JLS explicitly defines this as an exception to the rule. It allows this construct to support optimizations through the conditional compilation. For example, if( DEBUG){ System.out.println(" beginning task 1"); } Here, the DEBUG variable can be set to false in the code while generating the production version of the class file, which will allow the compiler to optimize the code by removing the whole if statement entirely from the class file.


 
Liutauras Vilda
Bartender
Pie
Posts: 2791
112
BSD VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In short, because in the loop it is supplied compile-time constant, in different words, boolean type literal, so Java 100% knows, that such code body would never execute, so it gives an error.

If you'd change such code to:
That would compile, because b is no longer compile time constant (as being evaluated during run time only), unless you declare it with 'final' modifier.

About the 'if' you provided explanation from JLS, that it is an exception to the rule, because of the reasons stated in JLS.


Not sure if that is typo, but that wouldn't compile only because of spaces in 'i + +', if you'd make it 'i++' that would compile. And it would compile for the same reasons, because 'i' isn't compile time constant.

Such code wouldn't compile, as 'false' is compile time constant, and such code result can be easily predicted by the compiler.
 
Jason Attin
Ranch Hand
Posts: 232
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks, much better now! I had absolutely no idea.
Yes apologies for the "+ +" that's just a copy and paste typo.
 
Liutauras Vilda
Bartender
Pie
Posts: 2791
112
BSD VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jason Attin wrote:thanks, much better now! I had absolutely no idea.
Yes apologies for the "+ +" that's just a copy and paste typo.

You're welcome and have nothing to apologise
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jason Attin wrote:Can somebody shed some light on it as I have absolutely no idea whatsoever please?

The "unreachable code" topic is one of the more popular ones in this forum. So using the search function you'll find plenty of topics about "unreachable code". Here is a list with a few of these topics with excellent explanations and illustrative code snippets:
  • return value for method.
  • Confused point about infinite loop
  • continue and break in a loop would generate compilation error?
  • Which of these is unreachable code?
  • Unreachable Code
  • Which is the first line to cause error?
  • Maybe "unreachable code" should be "dead code" in page 70, (Java OCA 8 Programmer I Study Guide)
  • Throwing a second exception question
  • System.exit() and unreachable code
  • good examples of unreachable code?

  • I think you know what to do this evening

    Hope it helps!
    Kind regards,
    Roel
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic