Dead code and unreachable code in java [puzzle]

For this puzzle, I have given a piece of code below. Try to identify the problems in code, if it is compiled in eclipse IDE.

Ads by Google
public class IdentifyProblemsInCode {

    public void howToDoInJava_method1() {
        System.out.println("how to do");
        return;
        System.out.println("in java");
    }

    public void howToDoInJava_method2() {
        System.out.println("how to do");
        if (true) {
            return;
        }
        System.out.println("in java");
    }

    public void howToDoInJava_method3() {
        System.out.println("how to do");
        while (true) {
            return;
        }
        System.out.println("in java");
    }
}

I am giving the answer of above puzzle in next section, but I would recommend you to try first yourself. Its for fun only.

Solution:

We all must have been faced compilation errors related to “Unreachable code” and some may have noticed “dead code warning“. Above puzzle is related to them only.

In first method i.e. howToDoInJava_method1(), second print statement is unreachable, so compiler will complain for oblivious reasons.

In second method howToDoInJava_method2(), second print statement is also unreachable, but strange compiler only warns you. We will later try to get the logic here.

In third method also i.e. howToDoInJava_method3(), second print statement is unreachable, so compiler will complain again. Strange !!

Reasoning

The unreachable code in method 2 is called “Dead code”. This is purely Eclipse compiler reported error, and if you will compile above class with “javac“, java inbuilt compiler will only complain for other two methods. [First and Third].

Quote from java language specification :

The idea is that there must be some possible execution path from the beginning of the constructor, method, instance initializer or static initializer that contains the statement to the statement itself. The analysis takes into account the structure of statements. Except for the special treatment of while, do, and for statements whose condition expression has the constant value true, the values of expressions are not taken into account in the flow analysis.”

What that means, is that the if block is not taken into account, since if you go through one of the paths of the if statement, you could reach second print statement. It all depends on compiler which determines this during compile time.

In other two statement, compiler has determined the un-reachability so it complains with error.

If we re-write second method again like this:

 public void howToDoInJava_method2() {
        System.out.println("how to do");
        if (true) {
            return;
        }
        else
         {
             return;
          }
        System.out.println("in java");
    }

Now, compiler determine that in no way it can reach to last print statement, so it again reports unreachable code.

If you still have some doubts, please write down in comment section. I will try to resolve your queries.

Happy Learning !!

Ads by Google

2 thoughts on “Dead code and unreachable code in java [puzzle]”

  1. As per the java specification quote(i.e specified by you), ‘IF condition expression’ aren’t taken into account during compilation but when i was compiling the below code then it was giving me an warning ‘Dead code’ for line number 2.
    if(1==2)
    System.out.println(“Dead code”);

    As per the quote, it should not give the warning.Please let me know that why this above code is giving an warning.
    it might be happen that i could not understand this quote correctly.

Note:- In comment box, please put your code inside [java] ... [/java] OR [xml] ... [/xml] tags otherwise it may not appear as intended.

Leave a Reply

Your email address will not be published. Required fields are marked *


5 − = three

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>