Java Puzzle: unreachable statement

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.

Ads by Google

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.

This Core Java tutorial was added on 09/10/2012.

«

»

Comments on "Java Puzzle: unreachable statement"

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

  2. Omprakash says:

    Short and crisp explanation!!

  3. nerd says:

    Good answers in return of this query with firm arguments and telling the whole thing concerning that.

    my page … nerd

  4. Billy Grades says:

    Very great!!!

  5. sdrkyj says:

    beautiful maze !

  6. Joe says:

    Sure, I will post more puzzles.

  7. vidya says:

    Good Job……

    thanks

  8. vikas says:

    nice and share more puzzles thanks

  9. Anonymous says:

    Thank You so much sir for such beautiful Explanation….

  10. kiran says:

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

  11. Mamatha. says:

    If possible please share some more puzzle like this.

  12. venkat says:

    good info knowledge excellent

  13. 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!

  14. anil says:

    thanks Joe. Really useful information

  15. Rahul sharma says:

    nice code

    thanks

  16. VJ says:

    Thanks Joe. Very interesting puzzle

  17. 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

  18. 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

  19. Anonymous says:

    Thanks a lot Joe

  20. prasanth jalasutram says:

    nice ones..

  21. Sunil says:

    good 1!!

  22. mohamed says:

    good info,thanks

  23. Anonymous says:

    very useful article…

    sneha

  24. satish says:

    as usual ., nice article :)

  25. Chandu says:

    Looks simple but thoughtful.

  26. Java dude says:

    These puzzles are also good for SCJP preparation.

  27. Andril A says:

    NICE ………. SEND MORE OF SUCH PUZZLES

  28. Anil says:

    As usual..Nice Article..

  29. Shaan says:

    This the good example of unreachable statment

  30. 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

  31. Subrat says:

    Thanks sir

  32. 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.

  33. Prabhat Kumar Kashyap says:

    Really amazing.
    Thanks Joe

  34. 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

    }

  35. M.S.Rayudu says:

    Nice one .. Thanks Joe!

  36. Anonymous says:

    Good Article Joe..

  37. 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 !

  38. Vishweshwar Nr Singh says:

    Its simply great illustration for it..:)

    Thanks Joe..

  39. Mukesh kumar Singh says:

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

  40. GOOTAM says:

    Good Article Joe.. keep the spirit up

  41. maddy says:

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

  42. Anonymous says:

    goood.. keep it up…!
    Thanks ….!

  43. Anonymous says:

    Nice one .. Thanks Joe!

  44. Teena says:

    good one , I like it.

  45. Anonymous says:

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

  46. Anonymous says:

    thanks joe

  47. harshawardhan says:

    Nice one, thanks !

  48. Anonymous says:

    Thanks Joe.

    :) ++

  49. Kamatchi Sundaram says:

    Good One…

  50. Murali Prashanth says:

    Thanx…

  51. Abhishek says:

    nyc 1

    keep posting many of such puzzles

  52. Spandana Vaddi says:

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

  53. Surendra Gurjar says:

    Thanks a tons………… :-)

  54. Surendra Gurjar says:

    Thanks a tones…..

  55. chetan says:

    Good info :)

  56. Santhosh Reddy says:

    Good one. Thanks Joe

  57. Anonymous says:

    Nice. Post many of such puzzles

Your Comment