Dead code and unreachable code in Java

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.

2. Solution – It has dead code

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 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 howToDoInJava_method3() also, second print statement is unreachable, so compiler will complain again.

Why !!

3. What is dead Code

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 whiledo, and for statements whose condition expression has the constant value true, the values of other expressions are not taken into account in the flow analysis”.

What that means, is that 'if' block is not taken into account while determining unreachable code. 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 'javac' again reports unreachable code for this second method as well.

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

Happy Learning !!

saigon has written 1440 articles

Leave a Reply