![]() ![]() Let us look into the second example with a break statement. When we compile this code, error is thrown at line 10 as print statement written after return becomes unreachable. The flow of execution inside foo() method is ended after returning the value. In this example, when foo() method is called, it returns value 10 to the main method. JavaCodeGeeks.java:11: error: missing return statement ![]() } Output JavaCodeGeeks.java:10: error: unreachable statement Let us see this with an example, with the return statement. ![]() They alter the flow of execution of a program and if any statement is placed right after it, there is a chance of code being unreachable. In Java language, keywords like return, break, continue are called Transfer Statements. 2.2 Statements after return, break or continue Note: if a and b are non-final, then the compiler will not raise any error as non-final variables are recognized only at runtime. While we compile the code, the error is thrown at line 7 indicating the body of the loop is unreachable. Hence, the body of the while loop is never executed and only the statement after the while loop gets executed. The compiler is able to evaluate the condition a>b to false. Final variables and its values are recognized by the compiler during compilation phase. In the above example, variables a and b are final. } Output JavaCodeGeeks.java:7: error: unreachable statement Now, let us see one more example which involves final variables in the code. If a statement is never executed, the compiler detects it and raises an error so as to avoid such statements. As there is an infinite while loop before the print statement of “hello”, this statement never gets executed at runtime. In this example, compiler raises unreachable statement error at line 8 when we compile the program using javac command. } Output JavaCodeGeeks.java:8: error: unreachable statement Consider the following code, Example 1 public class JavaCodeGeeks As the statement is placed right after the loop, this statement is never executed as the statements in the loop get executed repeatedly and the flow of execution is never terminated from the loop. In this scenario, a line of code is placed after an infinite loop.
0 Comments
Leave a Reply. |