goTo is a keyword in java, but not used.
It was added java 1.0 to be used in a later version of Java, but wasn’t implemented in java.
As goTo is not implemented, any attempt to use goTo will cause compilation error,
5 Alternate to goTo >
- We can use labeled break or
- labeled continue
- GotoFactory.getSharedInstance().getGoto().go(lineNumber); can be used to move to specified lineNumber in java source code.
- Also Exceptions help in serving alternate to goTo keyword. Read 5 keyword in java exception handling, Creating User defined checked and unchecked Exception and Exception propagation in java.
- Return statements in method also helps in serving alternate to goTo keyword in java. Read : What will happen when catch and finally block both return value, also when try and finally both return value
Let’s discuss above points in detail.
1. Labeled break statement>
So far we have used unlabeled break statements, but now we will learn how to use labeled break statements via program.
Program to demonstrate labeled break statement>
/** JavaMadeSoEasy.com */
public class StatementTest {
public static void main(String[] args) {
outerForLoop: // Labeled outer loop
for (int i = 0; i < 3; i++) {
System.out.println("outer i= " + i);
innerForLoop: // Labeled inner loop
for (int j = 0; j < 3; j++) {
if (j > 0) {
break outerForLoop;
}
System.out.println("inner i= " + i + ", j= " + j);
}// end inner loop
}// end outer loop
}
}
/* OUTPUT
outer i= 0
inner i= 0, j= 0
*/
|
2. Labeled continue statement>
So far we have used unlabeled continue statements, but now we will learn how to use labeled continue statements.
Program to demonstrate labeled continue statement>
/** JavaMadeSoEasy.com */
public class StatementTest {
public static void main(String[] args) {
outerForLoop: // Labeled outer loop
for (int i = 0; i < 3; i++) {
System.out.println("outer i= " + i);
innerForLoop: // Labeled inner loop
for (int j = 0; j < 3; j++) {
if (j > 0) {
continue innerForLoop;
}
if (i > 0) {
continue outerForLoop;
}
System.out.println("inner i= " + i + ", j= " + j);
}// end inner loop
}// end outer loop
}
}
/* OUTPUT
outer i= 0
inner i= 0, j= 0
outer i= 1
outer i= 2
*/
|
3. GotoFactory.getSharedInstance().getGoto().go(lineNumber); can be used to move to specified lineNumber in java source code.
1 public class GotoDemo {
2 public static void main(String[] args) {
3 int i = 3;
4 System.out.println(i);
5 i = i - 1;
6 if (i >= 0) {
7 GotoFactory.getSharedInstance().getGoto().go(4);
8 }
9
10 try {
11 System.out.print("Hell");
12 if (Math.random() > 0) throw new Exception();
13 System.out.println("World!");
14 } catch (Exception e) {
15 System.out.print("o ");
16 GotoFactory.getSharedInstance().getGoto().go(13);
17 }
18 }
19 }
|
Output of above program
3
2
1
0
Hello World!
As you can see, using GotoFactory.getSharedInstance().getGoto().go(4); is really bad way to go to specified line.
Example taken from : http://www.steike.com/code/useless/java-goto/
Please Read : 5 keyword in java exception handling, Creating User defined checked and unchecked Exception and Exception propagation in java.
5. Return statements in method also helps in serving alternate to goTo keyword in java.
Please Read : What will happen when catch and finally block both return value, also when try and finally both return value