While some commenters and downvoters argue that this isn't goto, the generated bytecode from the below Java statements really suggests that these statements really do express goto semantics.
Specifically, the do {...} while(true); loop in the second example is optimised by Java compilers in order not to evaluate the loop condition.
Jumping forward
label: {
// do stuff
if (check) break label;
// do more stuff
}
There are two constructs that allow you to do some of the things you
can do with a classic goto.
One more...
Matt Wolfe writes:
People always talk about never using a goto, but I think there is a
really good real world use case which is pretty well known and used..
That is, making sure to execute some code before a return from a
function.. Usually its releasing locks or what not, but in my case I'd
love to be able to jump to a break right before the return so I can do
required mandatory cleanup.
try {
// do stuff
return result; // or break, etc.
}
finally {
// clean up before actually returning, even though the order looks wrong.
}
The finally block always executes when the try block exits. This
ensures that the finally block is executed even if an unexpected
exception occurs. But finally is useful for more than just exception
handling — it allows the programmer to avoid having cleanup code
accidentally bypassed by a return, continue, or break. Putting cleanup
code in a finally block is always a good practice, even when no
exceptions are anticipated.
The silly interview question associated with finally is: If you return from a try{} block, but have a return in your finally{} too, which value is returned?
Java doesn't have goto, because it makes the code unstructured and unclear to read. However, you can use break and continue as civilized form of goto without its problems.
Jumping forward using break -
ahead: {
System.out.println("Before break");
break ahead;
System.out.println("After Break"); // This won't execute
}
// After a line break ahead, the code flow starts from here, after the ahead block
System.out.println("After ahead");
While some commenters and downvoters argue that this isn't goto, the generated bytecode from the below Java statements really suggests that these statements really do express goto semantics.
Specifically, the
do {...} while(true);
loop in the second example is optimised by Java compilers in order not to evaluate the loop condition.Jumping forward
In bytecode:
Jumping backward
In bytecode:
StephenC writes:
One more...
Matt Wolfe writes:
http://docs.oracle.com/javase/tutorial/essential/exceptions/finally.html
The silly interview question associated with finally is: If you return from a try{} block, but have a return in your finally{} too, which value is returned?
If you really want something like goto statements, you could always try breaking to named blocks.
You have to be within the scope of the block to break to the label:
I won't lecture you on why you should avoid goto's - I'm assuming you already know the answer to that.
Java doesn't have
goto
, because it makes the code unstructured and unclear to read. However, you can usebreak
andcontinue
as civilized form of goto without its problems.Jumping forward using break -
Output:
Jumping backward using continue
This will result in an infinite loop as every time the line
continue before
is executed, the code flow will start again frombefore
.Use a labeled break as an alternative to goto.