This is an interesting puzzle to test our understanding on java compiler. What happens when we compile below code blocks?
CODE A:
public void javapapers() { System.out.println("java"); return; System.out.println("papers"); }
CODE B:
public void javapapers() { System.out.println("java"); if(true) { return; } System.out.println("papers"); }
CODE C:
public void javapapers() { System.out.println("java"); while(true) { return; } System.out.println("papers"); }
Got the answer? More than answer let us try to understand the reasons behind it.
CODE A Output:
TestCompiler.java:5: unreachable statement
System.out.println(“papers”);
^
1 error
CODE A results in above compilation error and the reason is also clearly given as ‘unreachable statement’. We have a return followed by first system.out.println and after that line whatever code is there it will be unreachable and so this error.
CODE B Output:
Compiles successfully.
CODE C Output:
TestCompiler.java:7: unreachable statement
System.out.println(“papers”);
^
1 error
Code C results in above compilation error and the reason is unreachable statement. Same as in Code A.
We all know very well about unreachable statement will result in compilation error. It is a mandatory rule in java language specification that unreachable statement should be an error.
Code B and C are similar than Code A. If Code B compiles successfully, Code C should also compile.
The word Unreachable is defined in java language specification for every block like if, for, while, etc individually.
The block that is the body of a constructor, method, instance initializer, or static initializer is reachable…
like the above JLS goes on to define unreachable for all blocks including while, for, do, if, switch, etc separately.
The most general logic in these rules are, code surrounded by a block is unreachable if the conditional expression deciding it evaluates to false and then the block is decided as unreachable. Then it becomes an error.
Only for ‘if’ construct the rule is different. The conditional expression is not evaluated and decided. Whatever may be the expression inside the if condition, it is not evaluated to decide if the code block is reachable and so we will not get error even if we have a constant value ‘false’ as conditional expression in if statement. This is treated special to give a facility for the developer. Developers can use a flag variable in the if statement with default final values as false or true. Just by changing the flag variable from false to true or vice-versa developers can recompile the program and use it differently.
Comments are closed for "Java Puzzle: unreachable statement".
Nice. Post many of such puzzles
Good one. Thanks Joe
Good info :)
Thanks a tones…..
Thanks a tons………… :-)
Really good one , to understand in a better way. Post many puzzles as such to boost up our knowledge practically.
nyc 1
keep posting many of such puzzles
Thanx…
Good One…
Thanks Joe.
:) ++
Nice one, thanks !
thanks joe
Hi Joe, Thanks for explaining , but i am not getting the actual logic behind this. Can anyone clear this.
good one , I like it.
it’s really nice…
Thanks Joe and keep doing the good work..
Good Article Joe.. keep the spirit up
Nice explanation, but something is here to which I am unable to understand.
Its simply great illustration for it..:)
Thanks Joe..
This note book is quite long. Where do you buy that one? Leave alone the spiral – it must be more than 200 meter long :-).. Have a wonderful day, and thanks for your blog !
Nice one .. Thanks Joe!
Another example:
public int testUnreachableStmt(){
int x=1;
try
{
//some code here
return x;
}
catch(Exception ex)
{
//some code here
return x;
}
System.out.println(“UNREACHABLE STATEMENT”);———-unreachable
}
Really amazing.
Thanks Joe
Hi Joseph,
I just wanted some information regarding DocumentBuilderFactory,DocumentBuilder and Document in your simple terms.
I dont know where to put up this in your blog. So am posting it under the this topic.
It would be great if you can provide us with an option for entering user questions or doubts or feedbacks.
Thanks sir
Interesting thing is also that
boolean b=true;
public void javapapers() {
System.out.println(“java”);
while(b) {
return;
}
System.out.println(“papers”);
}
is also run very well
This the good example of unreachable statment
As usual..Nice Article..
NICE ………. SEND MORE OF SUCH PUZZLES
These puzzles are also good for SCJP preparation.
Looks simple but thoughtful.
as usual ., nice article :)
very useful article…
sneha
good info,thanks
good 1!!
nice ones..
I have some different explanation for this,
while(true) is a infinite loop so the code return after this will be unreachable.
similarly for(;;;) it is also infinite loop
if(true){
return “”;
}else{
return “”;
}
it will also give unreachable code exception during compile time,
there is nothing like special for treatment for ‘if’ by compiler
i dont get the exact idea about the difference between the if and while in code b and c.can u pls explain
Thanks Joe. Very interesting puzzle
nice code
thanks
thanks Joe. Really useful information
Thank you sooo much!
I have a computer science project due tomorrow and the only reason it wasn’t working because of an unreachable statement, now it works!!
you’re awesome!
keep it up!
good info knowledge excellent
If possible please share some more puzzle like this.
very nice…good.
please post these type of puzzles in java.
nice and share more puzzles thanks
Good Job……
thanks
Sure, I will post more puzzles.
beautiful maze !
Very great!!!
Short and crisp explanation!!
[…] mostly. That is where these kind of intriguing code gets cooked up. Long time back, I shared a Java puzzle on unreachable statement and it is similar to […]