aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes why the below highlighted line is not showing  unreachable compile error. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "why the below highlighted line is not showing  unreachable compile error." Watch "why the below highlighted line is not showing  unreachable compile error." New topic
Author

why the below highlighted line is not showing unreachable compile error.

pradeep singh
Ranch Hand

Joined: Oct 23, 2007
Posts: 339
Hi to all
I want to know why the below highlighted line is not showing unreachable compile error.





public class loopclass
{
public static void main(String...args)
{
int k=2;
outer: while(true)
{
++k;
inner: for(int j=6;j>2;j--)
{
k=8-j;
// if(j==3)continue inner;

break outer;

}
continue outer;//at this line compiler never reached in this program,then why not showing error.
}

System.out.println(k);
}


}


[ January 16, 2008: Message edited by: pradeep singh ]

[ January 16, 2008: Message edited by: pradeep singh ]
[ January 16, 2008: Message edited by: pradeep singh ]

SCJP 5.0(75%), SCWCD 5.0(88%)
Serg Masow
Ranch Hand

Joined: Dec 17, 2007
Posts: 49
Hi pradeep,

the expressions in the for loop are not the compile-time constants so they are validated first at runtime. The compiler doesn't know whether the for loop are entered or not. If the for loop is not entered the continue statement is reacheable well.

If you change the for loop like this you will get unreacheable statement error:



SCJP 6.0 [95%] OCP EJBD 6.0 [93%]
Sunny Jain
Ranch Hand

Joined: Jul 23, 2007
Posts: 433

Hi Let me try to explain this according to my understanding on the subject :

When we compile the program, Compiler do a flow analysis, in this flow analysis if its found any statement unreachable, it complains to us, this is unreachable code definition.

Now in the flow analysis compiler takes following into consideration :

Constructor
Method
instance initializer
static initializer

It does not take condition expression result of While, for,Do expressions.

So in your case although the code is unreachable but from compilor point of view if inner for block never executes( as result of conditional expression doesnt come into consideration) then the continue statement becomes reachable.

I hope this will clear your doubt.


Thanks and Regards,
SCJP 1.5 (90%), SCWCD 1.5 (85%), The Jovial Java, java.util.concurrent tutorial
Serg Masow
Ranch Hand

Joined: Dec 17, 2007
Posts: 49
the loop statement should be
for(;;) {
Ian Edwards
Ranch Hand

Joined: Aug 14, 2006
Posts: 107
The reason for this - from the compilers point of view - is that there could be any number of permutations possible in the loop, so it is unable to conclude whether the statement is unreachable in this case.

In more straight-forward cases it is able to advise, for example:
pradeep singh
Ranch Hand

Joined: Oct 23, 2007
Posts: 339
Hi
Sunny Jain said the below quoted rules ,but when i use these rule on the code shown/given by Ian Edward then according to Sunny it should not be a compiler error but it gives unreachable error.


Sunny says:

When we compile the program, Compiler do a flow analysis, in this flow analysis if its found any statement unreachable, it complains to us, this is unreachable code definition.

Now in the flow analysis compiler takes following into consideration :

Constructor
Method
instance initializer
static initializer

It does not take condition expression result of While, for,Do expressions.
Serg Masow
Ranch Hand

Joined: Dec 17, 2007
Posts: 49
exact rules for detecting an unreacheable statement gives �14.12 of java language specification there you can find an answer for your question.
 
wood burning stoves
 
subject: why the below highlighted line is not showing unreachable compile error.