This week's book giveaway is in the Agile and Other Processes forum.
We're giving away four copies of DevSecOps Adventures: A Game-Changing Approach with Chocolate, LEGO, and Coaching Games and have Dana Pylayeva on-line!
See this thread for details.
  • 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
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Tim Cooke
Sheriffs:
  • Rob Spoor
  • Liutauras Vilda
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
  • Piet Souris
Bartenders:
  • Stephan van Hulst

How to identify unreachable code ?

 
Ranch Hand
Posts: 39
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


In the above code, I thought as x is always zero and not updated and because of the continue statement,  System.out.println(j) will never be executed .so, I answered this question as "the code doesn't compile" but I was wrong.
May be is this because compiler thinks after line A there are chances of updating "x"?
what are the tips/tricks is to easily identify unreachable code?

P.S: Only zero is passed as an argument to the method in above code.
 
Marshal
Posts: 28304
95
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
All I can see is that the value of x never changes. I can't tell that it's always zero because, for example, the value of x passed to the method might be 42. But we don't see any code like that.

However... even if you and I could see that the value of x is zero and never changes, the compiler can't see that. The compiler isn't a programmer and it doesn't attempt to analyze code except in the most trivial ways, and those ways are specified in the JLS. For the exam you should certainly familiarize yourself with the ways that the compiler deems code unreachable, though. There's only a few of them.
 
Ranch Hand
Posts: 234
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You’re right. System.out.println(j) will never be executed; however, the compiler allows unreachable code caused by an if block in order to allow programmers to define "flag variables" for testing and debugging.

The JLS explains why the compiler allows this.

JLS
However, in order to allow the if statement to be used conveniently for "conditional compilation" purposes, the actual rules differ.


JLS
The rationale for this differing treatment is to allow programmers to define "flag variables" such as:

static final boolean DEBUG = false;

and then write code such as:

if (DEBUG) { x=3; }


 
Bartender
Posts: 15720
367
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In this case it would be the same even if a while-loop was used. x is not a compile time constant, so the compiler doesn't simplify expressions involving it.
 
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

Vidya Shivram wrote:what are the tips/tricks is to easily identify unreachable code?


Luckily for you, 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 which could probably be added to my certification book
  • 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 now

    Hope it helps!
    Kind regards,
    Roel
     
    What could go wrong in a swell place like "The Evil Eye"? Or with this tiny ad?
    Smokeless wood heat with a rocket mass heater
    https://woodheat.net
    reply
      Bookmark Topic Watch Topic
    • New Topic