aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Unreachable Code Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Unreachable Code " Watch "Unreachable Code " New topic
Author

Unreachable Code

naveen yadav
Ranch Hand

Joined: Oct 23, 2011
Posts: 384

Line #1 gives error as Unreachable code.But not for line #2. Why?

Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4387
    
    8

The Java Language Specification explains why it's designed that way:
As an example, the following statement results in a compile-time error:

while (false) { x=3; }

because the statement x=3; is not reachable; but the superficially similar case:

if (false) { x=3; }

does not result in a compile-time error. An optimizing compiler may realize that the statement x=3; will never be executed and may choose to omit the code for that statement from the generated class file, but the statement x=3; is not regarded as "unreachable" in the technical sense specified here.

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; }

The idea is that it should be possible to change the value of DEBUG from false to true or from true to false and then compile the code correctly with no other changes to the program text.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Unreachable Code