Page 1 of 1

About Question enthuware.ocajp.i.v7.2.1099 :

Posted: Wed Nov 19, 2014 7:25 pm
by SwitchCase
I was thinking since the code in the for block would never run it would be a compile time error. In what case(s) does the compiler consider non-reachable code an error? Thanks in advance.

Re: About Question enthuware.ocajp.i.v7.2.1099 :

Posted: Wed Nov 19, 2014 9:07 pm
by admin
The most important thing to look for is compile time constants such as final variables.
The compiler does not execute any code. It only analyzes the code. In the analysis, it can only consider compile time constants to make decisions about what the code "may" do at run time. Based on that if the compiler can determine that a piece of code will never execute, it generates an error.

Compiler cannot determine the value of a variable if it is not a constant. So even though we can see by looking at the code that the loop will never execute, the compiler cannot make that decision because it doesn't know the value of a variable.

One exception is the if condition. You will read about it in the explanations of a couple of questions soon.

HTH,
Paul.

Re: About Question enthuware.ocajp.i.v7.2.1099 :

Posted: Thu Feb 14, 2019 10:59 am
by OlgaAG
Why for( int i = 0; i< 0; i++) from question № 2.1279 is valid, but for (int i=10; i<0; i--) from this question is not valid?
i< 0; is false in both cases

Re: About Question enthuware.ocajp.i.v7.2.1099 :

Posted: Thu Feb 14, 2019 10:00 pm
by admin
What do you mean by "not valid"?
Both for( int i = 0; i< 0; i++) and for (int i=10; i<0; i--) compile fine.

Re: About Question enthuware.ocajp.i.v7.2.1099 :

Posted: Fri Feb 15, 2019 2:14 pm
by OlgaAG
Do I understand correctly that the for loop (int i = 10; i <0; i--) from question № 2.1099 and the for loop (int i = 0; i <0; i ++) x = 3; from question № 2.1279 both compile without any problems, because the compiler does not analyze comparison operators, but the JVM will not be executed them, because i <0 will mean false in both cycles?

Re: About Question enthuware.ocajp.i.v7.2.1099 :

Posted: Fri Feb 15, 2019 8:46 pm
by admin
That is correct.

Re: About Question enthuware.ocajp.i.v7.2.1099 :

Posted: Sun Apr 23, 2023 10:30 am
by steinov
I'm a bit confused about when unreachable code gives an compile error. The docs contain the following sentence:
This section is devoted to a precise explanation of the word "reachable." The idea is that there must be some possible execution path from the beginning of the constructor, method, instance initializer, or static initializer that contains the statement to the statement itself. The analysis takes into account the structure of statements. Except for the special treatment of while, do, and for statements whose condition expression has the constant value true, the values of expressions are not taken into account in the flow analysis.
Therefore I would expect the code to still compile when changing 'i<0' to 'true' in the for loop, but it doesn't. It gives an unreachable statement error for the println statement. What am I missing?

Re: About Question enthuware.ocajp.i.v7.2.1099 :

Posted: Sun Apr 23, 2023 1:09 pm
by admin
I don't think you are interpreting the statement correctly. It is actually saying that when the condition expression of while, do, and for statements has the constant value true, this value is taken into account in the flow analysis. So when you change i<0 to true, the compiler will take it into account and see that the last statement is unreachable and will generate a compilation error.

Re: About Question enthuware.ocajp.i.v7.2.1099 :

Posted: Sun Apr 30, 2023 6:17 am
by steinov
Ah, yeah I interpreted it the opposite way. I thought the loop is ignored in the flow analysis. But it actually means the loop is seen as an infinite loop (if the expression always evaluates to true) and any code after it will generate an unreachable statement compilation error.

For example the following will generate a compilation error for the print statement (//2).

Code: Select all

final int i  = -1;  //1
    while (i < 0) {
        return null;
    }
System.out.println(i);  //2
Another thing that I noticed; There is no compilation error if I remove the final from line //1, even though it is still effectively final. So I guess the compiler only does this for final values (and not effectively final)?