Win a copy of Spark in Action this week in the Open Source Projects 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

How to identify unreachable code ?

 
Ranch Hand
Posts: 39
1
  • Mark post as helpful
  • send pies
  • 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: 25669
69
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
  • 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: 12126
258
  • Mark post as helpful
  • send pies
  • 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
  • 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
      Bookmark Topic Watch Topic
    • New Topic