Win a copy of Rust Web Development this week in the Other Languages forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

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.
 
Sheriff
Posts: 26948
83
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: 231
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; }


 
Saloon Keeper
Posts: 13481
304
  • 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
     
    You showed up just in time for the waffles! And this tiny ad:
    Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
    https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    reply
      Bookmark Topic Watch Topic
    • New Topic