Unreachable Code
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Why some lines are unreachable but compiles but some does not? How do we know? Please explain. Thanks.
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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.
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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.
SCJP 1.4, SCWCD 1.4 - Hints for you, Certified Scrum Master
Did a rm -R / to find out that I lost my entire Linux installation!
-
1 -
-
Number of slices to send:Optional 'thank-you' note:
-
-
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
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
what is 15.28. Constant Expressions
and 14.21. Unreachable Statements
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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.
-
1 -
-
Number of slices to send:Optional 'thank-you' note:
-
-
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
-
2 -
-
Number of slices to send:Optional 'thank-you' note:
-
-
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
| Don't play dumb with me! But you can try this tiny ad: Paul Wheaton's 16th Kickstarter: Gardening playing cards for gardeners and homesteaders https://coderanch.com/t/889615/Paul-Wheaton-Kickstarter-Gardening-playing |








