• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Possible unreachable code inside for loop

 
Ranch Hand
Posts: 234
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 234
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
AH sorry, forgot to say that code assumes that the value of x passed to the method is 0.
 
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 234
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic