About Question enthuware.ocajp.i.v7.2.1099 :
Moderator: admin
-
- Posts: 1
- Joined: Wed Nov 19, 2014 7:21 pm
- Contact:
About Question enthuware.ocajp.i.v7.2.1099 :
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.
-
- Site Admin
- Posts: 10078
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1099 :
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.
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.
If you like our products and services, please help us by posting your review here.
-
- Posts: 10
- Joined: Thu Jan 03, 2019 3:08 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1099 :
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
i< 0; is false in both cases
-
- Site Admin
- Posts: 10078
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1099 :
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.
Both for( int i = 0; i< 0; i++) and for (int i=10; i<0; i--) compile fine.
If you like our products and services, please help us by posting your review here.
-
- Posts: 10
- Joined: Thu Jan 03, 2019 3:08 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1099 :
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?
-
- Site Admin
- Posts: 10078
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1099 :
That is correct.
If you like our products and services, please help us by posting your review here.
-
- Posts: 19
- Joined: Wed Feb 08, 2023 3:11 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1099 :
I'm a bit confused about when unreachable code gives an compile error. The docs contain the following sentence:
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?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.
-
- Site Admin
- Posts: 10078
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1099 :
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.
If you like our products and services, please help us by posting your review here.
-
- Posts: 19
- Joined: Wed Feb 08, 2023 3:11 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1099 :
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).
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)?
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
Who is online
Users browsing this forum: No registered users and 13 guests