Java Dead Code Warning
Since the release of Java, it has been extensively used by programmers. We can attribute its success to Java’s exclusive and powerful features as a programming language.
Java is a platform-independent, fully object-oriented language developed to overcome the shortcomings of previous languages.
If you know Java, you might have already been surprised by its unique abilities. Owing to its popularity, we have several interactive development environment software in the market.
This softwares sometimes implement and improvise the Java runtime environment as well. One such improvisation is the Dead Code
warning.
The Dead Code
warning is specific to Eclipse IDE and is not a part of the usual java compiler. In this article, we will understand the concept of dead code.
We will also understand a similar term called unreachable code in Java. You should not confuse yourself with these two terms as both are different.
We will see the difference between both as we proceed in the article.
the Dead Code
Warning in Java
Sometimes, while working with Java in Eclipse, we encounter a warning message displaying the Dead Code.
As the name suggests, the Dead Code in Java is a section of code that practically doesn’t affect the execution results.
The dead code is a section that might or might not get executed but surely has no overall effect on the execution path and results.
There can be multiple reasons for the code not being executed or not affecting the program’s functionality.
For example, we might have put an else
following an if
such that the conditional inside the it
is always inevitably true. For this reason, the else
is practically never executed, making it a dead code.
Another example is to use a return statement inside a for
loop without any condition. In this case, our code control will reach the loop and return from it without executing the loop variable update statement.
Therefore, we will have a dead code warning in this case. As a rule, we can say that any code that is ineffective and plays no role in the execution results is called a Dead Code.
You should note that the dead code warning is not a part of the Java compiler and is purely native to Eclipse IDE (there might be other IDEs implementing similar concepts).
Moreover, the program is still executed because the Dead Code is a warning and not an error in Java. Let us understand the above-given examples with the help of Java codes written using an Eclipse IDE.
import java.util.*;
public class deadcode {
public static void main(String[] args) {
if (true) {
System.out.println("Inside if");
}
// Dead code
else {
System.out.println("Inside else");
}
}
}
The else
conditional will not affect the results in the above Java program because the if
conditional is always true
.
Therefore, we will get a dead code
warning. Let us see if the warning is still displayed if we compile the same java code using javac
.
Output:
stark@stark:~/eclipse-workspace-java/Aditya/src$ javac deadcode.java
stark@stark:~/eclipse-workspace-java/Aditya/src$ java deadcode
Inside if
We can see that no warning was displayed when we compiled and executed the code outside Eclipse.
Let us see another example of the dead code
warning.
import java.util.*;
public class deadcode {
public static void main(String[] args) {
for (int i = 0; i < 10; i++) {
System.out.println("Inside for");
return;
}
}
}
The above Java code again displays the dead code warning in Eclipse. In this code, inside the for
loop, we have used a return statement executed when the loop is executed for the first time.
This will control return, and the i++
will have no effect. The loop variable update statement i++
in the above loop is executed after performing all operations inside the loop.
Thus, we get a dead code
warning in Eclipse. However, the same code will not produce any warning when executed using javac.
Difference Between Dead Code and Unreachable Code in Java
An unreachable code is such a piece of code that will never be executed irrespective of any execution flow path.
At first, you might think that it is the same as the dead code. However, the difference between a dead code is that it might or might not get executed, while an unreachable code will never be executed.
The dead code does not affect the execution result of the Java Program. On the other hand, the unreachable code is never reached by any possible control flow path.
For example, a simple error that causes an unreachable code is when we place any statement after a return statement.
We can logically imply that a return statement will cause the execution to return from the program.
Therefore, our program control will never reach the statements after a return statement. This will make an unreachable code error.
There is yet another important difference between a dead code and an unreachable code that we should be careful about.
A dead code is just a warning message native to Eclipse or another compiler. It is not displayed in the javac core java compiler.
On the other hand, the unreachable code is an error reported by the javac Java compiler. It is officially included in java as an error.
The following example demonstrates the unreachable code error.
import java.util.*;
public class deadcode {
public static void main(String[] args) {
System.out.println("Hello, World!");
return;
System.out.println("This is unreachable");
}
}
It is displayed as an error in the Eclipse and Javac compiler. When compiled with javac following error message is displayed.
Output:
stark@stark:~/eclipse-workspace-java/Aditya/src$ javac deadcode.java
deadcode.java:9: error: unreachable statement
System.out.println("This is unreachable");
^
1 error
Conclusion
The dead code in Java is a common mistake programmers experience. We often overlook the situations when we mistakenly put statements that do not affect our program results.
These mistakes, though insignificant, do not carry a good image for you being a programmer. Therefore, now that you have understood the dead code in Java, you shall be able to avoid such situations.
And if you are already getting the warning of the dead code in your IDE, you shall have a better idea of why it is there and how you can deal with it.