I\'m confused about this. Most of us have been told that there isn\'t any goto statement in Java.
But I found that it is one of the keywords in Java. Where can it be
http://docs.oracle.com/javase/specs/jvms/se7/html/jvms-6.html#jvms-6.5.goto
If you have been told that there is no goto statement in Java you have been fooled. Indeed, Java consists two layers of 'source' code.
No, thankfully, there isn't goto
in Java.
The goto
keyword is only reserved, but not used (the same goes for const
).
It is important to understand that the goto
construct is remnant from the days that programmers programmed in machine code and assembly language. Because those languages are so basic (as in, each instruction does only one thing), program control flow is done completely with goto
statements (but in assembly language, these are referred to as jump or branch instructions).
Now, although the C language is fairly low-level, it can be thought of as very high-level assembly language - each statement and function in C can easily be broken down into assembly language instructions. Although C is not the prime language to program computers with nowadays, it is still heavily used in low level applications, such as embedded systems. Because C's function so closely mirrors assembly language's function, it only makes sense that goto
is included in C.
It is clear that Java is an evolution of C/C++. Java shares a lot of features from C, but abstracts a lot more of the details, and therefore is simply written differently. Java is a very high-level language, so it simply is not necessary to have low-level features like goto
when more high-level constructs like functions, for, for each, and while loops do the program control flow. Imagine if you were in one function and did a goto
to a label into another function. What would happen when the other function returned? This idea is absurd.
This does not necessarily answer why Java includes the goto
statement yet won't let it compile, but it is important to know why goto
was ever used in the first place, in lower-level applications, and why it just doesn't make sense to be used in Java.
An example of how to use "continue" labels in Java is:
public class Label {
public static void main(String[] args) {
int temp = 0;
out: // label
for (int i = 0; i < 3; ++i) {
System.out.println("I am here");
for (int j = 0; j < 20; ++j) {
if(temp==0) {
System.out.println("j: " + j);
if (j == 1) {
temp = j;
continue out; // goto label "out"
}
}
}
}
System.out.println("temp = " + temp);
}
}
Results:
I am here // i=0
j: 0
j: 1
I am here // i=1
I am here // i=2
temp = 1
Yes is it possible, but not as nice as in c# (in my opinion c# is BETTER!). Opinions that goto always obscures software are dull and silly! It's sad java don't have at least goto case xxx.
Jump to forward:
public static void main(String [] args) {
myblock: {
System.out.println("Hello");
if (some_condition)
break myblock;
System.out.println("Nice day");
}
// here code continue after performing break myblock
System.out.println("And work");
}
Jump to backward:
public static void main(String [] args) {
mystart: //here code continue after performing continue mystart
do {
System.out.println("Hello");
if (some_condition)
continue mystart;
System.out.println("Nice day");
} while (false);
System.out.println("And work");
}