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

Possible unreachable code inside for loop

 
Jason Attin
Ranch Hand
Posts: 232
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI guys, I have a question about the following code please (Glenn, Mitchell. OCAJP Oracle Certified Associate Java SE 8 Programmer Practice Exams (Kindle Locations 14185-14189). Enthuware. Kindle Edition. ):

This is supposed to print 1 2 3 4. Now, I have read (must admit not religiously but gave a good look) at the links that Roel De Nijs kindly provided me with in a different thread (on unreachable code that is), but I couldn't find anything similar to this, so I thought I'd post it.
I know by now that Java relazes the rule of unreachable code when it comes down to if statement, but here I can see that the statement will never be reached, yet the code compiles OK. Why is that please? thanks!

 
Jason Attin
Ranch Hand
Posts: 232
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
AH sorry, forgot to say that code assumes that the value of x passed to the method is 0.
 
Dave Tolls
Ranch Hand
Posts: 2109
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The compiler doesn't check what calls are made to a method.
Consequently, as far as it's concerned, 'x' can be any int value.
 
Henry Wong
author
Marshal
Pie
Posts: 22116
88
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Or to elaborate more on what Dave stated. The compiler uses compile time constants to determine reachability, as it ... well ... runs during compile time...

Henry
 
Jason Attin
Ranch Hand
Posts: 232
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Right, so are we effectively saying that if there is some unreachable code in a method it will compile anyway? And that, as a consequence, unreachable code can be deemed as such only if it's where the main method is? I'm just trying to find some kind of rule here
 
Henry Wong
author
Marshal
Pie
Posts: 22116
88
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jason Attin wrote:Right, so are we effectively saying that if there is some unreachable code in a method it will compile anyway? And that, as a consequence, unreachable code can be deemed as such only if it's where the main method is? I'm just trying to find some kind of rule here


The definition of reachable code is very highly defined by the Java Language Specification. Additionally, the behavior of the unreachable code is also highly defined. And finally, it also depends on some other complex rules, such as what is a compile time constant.

https://docs.oracle.com/javase/specs/jls/se8/html/jls-14.html#jls-14.21

While it is probably a good idea to come up with a couple of anecdotal rules to help you remember -- you have to remember that it is only a memory aid, and may not be true in all cases.

Henry
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jason Attin wrote:Why is that please?

That's very easy! Because its "unreachability" depends on a runtime value (passing 0 as value to variable x); if another value is passed, the line will be executed. The compiler only cares about what is known at compile-time, the compiler is not interested at the beautiful magic happening at runtime. That means that every compiler error is the result of what you have typed without executing any line of code.

And this is really a very, very, very important rule (it might even be the most important rule you need to know for the exam): The compiler doesn't execute any code! So every compiler error you get, is because the compiler knows something is wrong without executing any line of code.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic