Unreachable Code or Statement in JavaAn unreachable code or statement in Java is a common problem among Java beginners. It is a compile-time error. A lot of novice developers confuse the error with dead code- another Java-related phenomenon. Although the two are similar by manifestation, there are a slight difference between two that we will discuss in this section. Other than that, we will find that the most common reasons for compiler returning an unreachable code statement are and discover some easy fixes to get code and running again. What is Unreachable code?By definition, an unreachable statement is a statement that will not be executed by a compiler when we execute ready-to-deploy code. An unreachable code return statement is typically a sign of a logical error within the program. Although there are several reasons why we end up with such a statement, in all cases, unreachable code is redundant, clutters of the program, and should be avoided at all costs. It can happen for various reasons, such as:
Why should we avoid unreachable statements?There are a few reasons why we should avoid unreachable statements in Java code:
Why we get Unreachable code statements?The good news is, it's easy to trace the cause of unreachable code issues. There are the following three reasons why the compiler keeps returning errors:
ReturnA return statement is a part of the transfer keyword group, meaning that it terminates method. It is helpful for separating functions and keep code readable and clean. However, we cannot add new statements to the function after return = true, trying to continue the function after the keyword will give an Unreachable code error. ReturnStatement.java Output: error: unreachable statement System.out.println("My code will run"); ^ 1 error Break StatementsBreak statements are another type of keyword. We need to be careful while writing Java functions. By the definition, the break keyword is used to terminate a loop. In the example below, while we exit from the for loop, we will no longer be able to execute the statement at line 8. Thus, the compiler will show an unreachable statement error. BreakStatement.java Output: error: unreachable statement System.out.println("After break"); ^ 1 error Continue StatementsContinue is a loop control keyword used to reiterate actions. Whenever we want the execution of a loop to start from scratch, add continue to the code. The statement is useful to help developers choose which statements of the loop they want to reiterate and the ones they'd not put in the iteration. Although continue is a straightforward keyword to use, not having full understanding of how it works leads developers to the unreachable code trap. Since, after encountering a continue, it will reiterate the loop, the keyword will not be able to reach the statements that follow it. ContinueStatement.java Output: error: unreachable statement System.out.println("Coding after continue"); A scenario that's similar to the examples of "break" and "continue" keyword use cases is that of an infinite loop. When designing an infinite loop, a developer should remember that no statement after it will ever run. Thus, if we do not break the loop, all the code written after will be unreachable. InfiniteloopStatement.Java Output: error: unreachable statement System.out.println("Nice to see you"); ' ConclusionTo avoid unreachable code in Java, it is important to ensure a flow to all system statements and handle keywords and loops properly. Double-checking the code is the best way to prevent such mistakes. Next TopicWhat is Architecture Neutral in Java |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India