Java Puzzle: unreachable statement

09/10/2012

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 posted on 09/10/2012.
Ads by Google

57 comments on “Java Puzzle: unreachable statement

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

  2. nyc 1

    keep posting many of such puzzles

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

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

  5. Nice explanation, but something is here to which I am unable to understand.
    So can u please send me the some more explanation on my ID mks.ps143@gmail.com.

    Anyone!!…

  6. Its simply great illustration for it..:)

    Thanks Joe..

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

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

    }

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

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

  11. NICE ………. SEND MORE OF SUCH PUZZLES

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

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

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

  15. If possible please share some more puzzle like this.

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

  17. Thank You so much sir for such beautiful Explanation….

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

    my page … nerd

  19. Pingback: Java Puzzle: Commented Code Compiles

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>