AddThis Social Bookmark Button

It may sound stupid Java, But seriously, how helpful is it  having a compiler complaining about code which is unreachable ? Well, a lot.

Java does come with a promise of writing code which is easily understood, thus leading to easy maintainability. Not sure what I am referring to ? Think pointers. Pointers in programming language though made languages powerful ( C and C++) , It is a difficult situation to handle , ever for the experienced developers ( A developer is like "Hey hang on I see pointer to pointer to pointer, gotta understand this first" ) , Well Java did solved the problem.

The same is true about unreachable code. Java also came up with promise to have beautiful code , un-initialized variables ( both warning and error ) and so is unreachable code.

Lets explore few of the possibility when you might see unreachable code.

      

return ;
System.out.println("");

// make sense , if you return , why would you print something.
boolean status=false;
if(x){
		return true;
}else{
		return false;
}
return status;
// well, with the if and else you covered all the branches , what on earth would you do something after.

Now Java is being intelligent here, trying to say that you actually have covered all the possible scenarios when you want to return true or false, having a default ( status ) is unnecessary. But consider this following code snippet.
 


boolean status=false;
		if(x){
		return true;
		}else{
		  if(y){
			  System.out.println("something to do here");
		 }
		}
		return status;
// Do you think you covered all the branches ?

The above code is just fine, no problem ( neither I or the Java are  really making it up , check yourself does it cover all the branching conditions ?

But look at the following two code snippets the if block and the while block

// the if with false      
if(false){
	System.out.println("some");
}
// the while with false
while(false){
	System.out.println("falsing");
}


Well the if works without problem , but the while with false will not work , will complain about unreachable code. Why ? I will get back on this if I see my readers could not find answer to this.

Lets explore more.
 

for(int i=0;i<10;i++){
            System.out.println("do this ");
            continue;
            System.out.println("do this, don't see why"); // I feel stupid
        }


But should I be stupid If I write this below ? No, there is nothing stupid about it. There are chances when the print code executes.
 

for(int i=0;i<10;i++){
            System.out.println("do this ");
            if(i==2){
            continue;
            }
            System.out.println("I see a chance of running,so no stupidity");
        }



Or how about this, eh ? (  The Canadian eh ? )

for(int i=0;i<10;i++){
			System.out.println("do this ");
			if(i==2){
				continue;
			}else{
				break;
			}
			System.out.println("do this, and think");
		}

WTF ? , well it is unreachable,there is no case why you see the sysout should print.

for(int i=0;i<10;i++){
			System.out.println("do this ");
			if(i==2){
				continue;
			}else{
				System.out.println("i am doing just fine here");
			}
			System.out.println("do this");
		}

It is just okey.

I will add more later on, especially the ones that deals with Exceptions. Signing off....