This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes About unreachable statements with if Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "About unreachable statements with if" Watch "About unreachable statements with if" New topic
Author

About unreachable statements with if

Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

It's already discussed. But it's is not satisfactory... Somebody explain it further! I got it here.

I made some modification there!



|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Anuradha Prasanna
Ranch Hand

Joined: Mar 09, 2006
Posts: 115
About the below code,

try {
if(true) //line 1
throw new Exception();
System.out.println("A"); // line 2
} catch(Exception e) {}

According to my understanding, since the if(true) in line 1 is not evaluated at compile time(the expression inside if() is not evaluated at compile time), line 2 is considered reachable at compile time.
But at runtime, line 2 is never reachable.


SCJP 6.0 90%
Venkata Kumar
Ranch Hand

Joined: Apr 16, 2008
Posts: 110

Please see the following Henry comments in the thread

The failure of the compiler to correctly detect unreachable code, when the "if" condition is involved, is not due to the optimizer. It is actually defined in the JLS to be like that.

In the Java Language Specification, it actually defines what should be done (hypothetical) and what is done (actual) for the "if" statement. The unreachability tests for the "if" statement is purposely weaken, in order to allow for conditional code. This way, you can turn on or off debugging code, and not have the compiler complain the code inside the "if" or "else" block is unreachable.


SCJP 5.0, SCWCD 5, preparing for SCDJWS
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Anuradha Prasanna wrote:About the below code,

try {
if(true) //line 1
throw new Exception();
System.out.println("A"); // line 2
} catch(Exception e) {}

According to my understanding, since the if(true) in line 1 is not evaluated at compile time(the expression inside if() is not evaluated at compile time), line 2 is considered reachable at compile time.
But at runtime, line 2 is never reachable.


Ok Anuradha Prasanna, Then what is the difference between line 6 and line 26 in my coding? Both are compile time constants???
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Anybody here???
Anuradha Prasanna
Ranch Hand

Joined: Mar 09, 2006
Posts: 115
At line 6,
if(true) --> the expression inside if() loop(in this case, "true") is not evaluated at compile time.
so the line 9, "System.out.println("A"); is reachable.

At line 26,
if(9<10) --> "9<10" is not evaluated at compile time.
To make this clear,
if you change line 26 as,
if(10<9) l = 10;(assuming l is initialized before), this will compile, but at runtime, "10>9" is evaluated as "false" and "l=10" is not reached.

Hope your doubts are cleared now.
Vivek K Singh
Ranch Hand

Joined: Dec 22, 2009
Posts: 85

the thread post by Venkat defines it in clear terms "In the Java Language Specification, it actually defines what should be done (hypothetical) and what is done (actual) for the "if" statement.

The unreachability tests for the "if" statement is purposely weaken, in order to allow for conditional code. This way, you can turn on or off debugging code, and not have the compiler complain the code inside the "if" or "else" block is unreachable."

Also look at this:


Here as well else part will never get executed, but the weaken unreachability tests for the "if" statement will not force you to change it showing a compile time error.


SCJP 6
 
jQuery in Action, 2nd edition
 
subject: About unreachable statements with if