Java Puzzle: unreachable statement

Last modified on December 8th, 2014 by Joe.

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.

Puzzle Solution

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.

Okay what is strange here?

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 on "Java Puzzle: unreachable statement"

  1. Anonymous says:

    Nice. Post many of such puzzles

  2. Santhosh Reddy says:

    Good one. Thanks Joe

  3. chetan says:

    Good info :)

  4. Surendra Gurjar says:

    Thanks a tones…..

  5. Surendra Gurjar says:

    Thanks a tons………… :-)

  6. Spandana Vaddi says:

    Really good one , to understand in a better way. Post many puzzles as such to boost up our knowledge practically.

  7. Abhishek says:

    nyc 1

    keep posting many of such puzzles

  8. Murali Prashanth says:

    Thanx…

  9. Kamatchi Sundaram says:

    Good One…

  10. Anonymous says:

    Thanks Joe.

    :) ++

  11. harshawardhan says:

    Nice one, thanks !

  12. Anonymous says:

    thanks joe

  13. Anonymous says:

    Hi Joe, Thanks for explaining , but i am not getting the actual logic behind this. Can anyone clear this.

  14. Teena says:

    good one , I like it.

  15. maddy says:

    it’s really nice…
    Thanks Joe and keep doing the good work..

  16. GOOTAM says:

    Good Article Joe.. keep the spirit up

  17. Mukesh kumar Singh says:

    Nice explanation, but something is here to which I am unable to understand.

  18. Vishweshwar Nr Singh says:

    Its simply great illustration for it..:)

    Thanks Joe..

  19. David Svarrer says:

    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 !

  20. M.S.Rayudu says:

    Nice one .. Thanks Joe!

  21. Anonymous says:

    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

    }

  22. Prabhat Kumar Kashyap says:

    Really amazing.
    Thanks Joe

  23. Karthik says:

    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.

  24. Subrat says:

    Thanks sir

  25. Nirav Modi says:

    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

  26. Shaan says:

    This the good example of unreachable statment

  27. Anil says:

    As usual..Nice Article..

  28. Andril A says:

    NICE ………. SEND MORE OF SUCH PUZZLES

  29. Java dude says:

    These puzzles are also good for SCJP preparation.

  30. Chandu says:

    Looks simple but thoughtful.

  31. satish says:

    as usual ., nice article :)

  32. Anonymous says:

    very useful article…

    sneha

  33. mohamed says:

    good info,thanks

  34. Sunil says:

    good 1!!

  35. prasanth jalasutram says:

    nice ones..

  36. Gajendra says:

    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

  37. krishnaveni says:

    i dont get the exact idea about the difference between the if and while in code b and c.can u pls explain

  38. VJ says:

    Thanks Joe. Very interesting puzzle

  39. Rahul sharma says:

    nice code

    thanks

  40. anil says:

    thanks Joe. Really useful information

  41. Ishita says:

    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!

  42. venkat says:

    good info knowledge excellent

  43. Mamatha. says:

    If possible please share some more puzzle like this.

  44. kiran says:

    very nice…good.
    please post these type of puzzles in java.

  45. vikas says:

    nice and share more puzzles thanks

  46. vidya says:

    Good Job……

    thanks

  47. Joe says:

    Sure, I will post more puzzles.

  48. sdrkyj says:

    beautiful maze !

  49. Billy Grades says:

    Very great!!!

  50. Omprakash says:

    Short and crisp explanation!!

  51. […] 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 […]

Comments are closed for "Java Puzzle: unreachable statement".