• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Unreachable Code

 
Alan Blass
Ranch Hand
Posts: 161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi! I have:



Why some lines are unreachable but compiles but some does not? How do we know? Please explain. Thanks.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The compiler generally does static, but not dynamic, analysis. By that I mean that it doesn't try to keep track of things that can change. The first two lines are only unreachable if you make deductions based on the value of the flag parameter.

For the later case, line 27 is unreachable because both branches of the preceding if statement will cause it to be skipped. The compiler doesn't have to track variables to work that out. In theory it could work out line 23 is unreachable as well, but if(true) and if(false) are specifically treated as special cases in the Java Language Specification because they can be useful in certain circumstances.
 
Joe Harry
Ranch Hand
Posts: 10128
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That bit of code is really unreadable as well.

You can work that out by yourself as to why that bit of code is unreachable! Assume the flag to be true and work it through. You will see it for yourself.
 
Alan Blass
Ranch Hand
Posts: 161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks!
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why some lines are unreachable but compiles but some does not? How do we know?

Java does allow unreachable code caused by if statements, even if you don't use a variable but a constant.

A few examples. The statement in between the curly braces will never be reached (dead code), but not an unreachable code compiler error.


But what happens if you change the if statement into a while statement? Let's find out:


Both examples now result in an unreachable code compiler error.

And what do you think of this one?


And now let's add some if-statement to the while loop. What will the result of this snippet be?


Hope it helps!
Kind regards,
Roel
 
Sergej Smoljanov
Ranch Hand
Posts: 467
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
for robust understand what is reachable/unreachable you should know:
what is 15.28. Constant Expressions
and 14.21. Unreachable Statements
 
Prathima gaitonde
Ranch Hand
Posts: 130
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All, I have the same problem, I cant get the concept of reachable code and unreachable code,
as Roel said:
if(2<1){dosomething} is fine!!!

while(2<1){dosomething} compiler error???

I had been to JLS page http://docs.oracle.com/javase/specs/jls/se8/html/jls-14.html#jls-14.21 I always found that the statements there, are more confusing (At least for me).

1> What is an empty block?
2> What is an empty statement?
3> What does contained statement means? Can anyone simplify these rules to normal/understandable level? please!!!

Notes on constants specified in http://docs.oracle.com/javase/specs/jls/se8/html/jls-15.html#jls-15.28

1>A constant expression is always treated as FP-strict? is this means 12 is a floating point?

Sorry If I have got wrong anywhere, I am a kid(new born) in this field.

Kind regards,
Prathima.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Prathima gaitonde wrote:I always found that the statements there, are more confusing (At least for me).

Exactly the reason why I have never read the JLS while preparing for a certification exam. And also the reason why I advice beginners not to use/read JLS.

Prathima gaitonde wrote:1> What is an empty block?



Prathima gaitonde wrote:2> What is an empty statement?



Prathima gaitonde wrote:3> What does contained statement means?


In the context of a while statement, the contained statement is probably the body of the loop, so everything inside the curly braces (in this case: // do something)

Prathima gaitonde wrote:1>A constant expression is always treated as FP-strict? is this means 12 is a floating point?

It's related to the strictfp modifier. You use the strictfp keyword when you want floating point calculations in some class or method to adhere a specific standard (IEEE 754). But that's out of scope for the OCAJP7 exam, so don't bother.

Hope it helps!
Kind regards,
Roel
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Prathima gaitonde wrote:Hi All, I have the same problem, I cant get the concept of reachable code and unreachable code,
as Roel said:
if(2<1){dosomething} is fine!!!

while(2<1){dosomething} compiler error???

If you have a while statement with a condition which always evaluates to false (2 can never be smaller than 1), the body of the loop is unreachable and you'll get a compiler error.

These all don't compile:



Why is this not with an if-statement. I can illustrate with an easy example. Let's assume I have a boolean flag DEBUG. When I'm developing my application, I set this value to true and I get a bunch of messages sent to the console for debugging purposes. When this application is released, I'll set this value to false. Disclaimer: when you develop a real application you should use a logging framework instead of this approach

So no problem here: the print-statement is always reachable. Now it's time to release the application and set the flag DEBUG to false.

This code would not compile anymore, because the print-statement would now be unreachable. Uh-oh! It was compiling in development and test, but not anymore in production That's why there is a difference between an if-statement and a loop (while, for, do while)


Hope it helps!
Kind regards,
Roel
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic