Dead code and Unreachable code in Java

Learn to identify dead code and unreachable code in Java. It may be asked in your Java interview coding problems as a puzzle.

1. Dead code and Unreachable code

To understand the concept of dead code, in this puzzle, I have given a piece of code below. Try to identify the problems in code, if it is compiled in an IDE.

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");
    }
}

We get the following compilation errors:

PS C:\Users\lokesh\IdeaProjects\Core-Java\src\main\java\com\howtodoinjava\puzzles> javac .\IdentifyProblemsInCode.java

.\IdentifyProblemsInCode.java:8: error: unreachable statement
    System.out.println("in java");
    ^
.\IdentifyProblemsInCode.java:24: error: unreachable statement
    System.out.println("in java");
    ^
2 errors

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

2. Compilation Errors

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

  • In the first method howToDoInJava_method1(), the second print statement is unreachable, so the compiler will complain for oblivious reasons.
  • In the second method howToDoInJava_method2(), the second print statement is also unreachable, but the strange thing is that compiler only warns. We will later try to get the logic here.
  • In the third method howToDoInJava_method3() also, the second print statement is unreachable, so the 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 the above class with “javac“, Java inbuilt compiler will only complain for the 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 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 the second print statement. It all depends on the compiler which determines this during compile time.

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

If we re-write the 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, the compiler determines 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 them down in the comment section. I will try to resolve your queries.

Happy Learning !!

Sourcecode on Github

Comments

Subscribe
Notify of
guest
6 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.