This week's book giveaway is in the Cloud/Virtualization forum.
We're giving away four copies of Building Blockchain Apps and have Michael Yuan on-line!
See this thread for details.
Win a copy of Building Blockchain Apps this week in the Cloud/Virtualization 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
  • Paul Clapham
  • Liutauras Vilda
  • Knute Snortum
  • Bear Bibeault
Sheriffs:
  • Devaka Cooray
  • Jeanne Boyarsky
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • salvin francis
Bartenders:
  • Tim Holloway
  • Piet Souris
  • Frits Walraven

About unreachable statements with if

 
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's already discussed. But it's is not satisfactory... Somebody explain it further! I got it here.

I made some modification there!

 
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 110
Firefox Browser MySQL Database Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anybody here???
 
Anuradha Prasanna
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 85
Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
He baked a muffin that stole my car! And this tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!