wood burning stoves 2.0*
The moose likes Java in General and the fly likes goto equivalent in java? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "goto equivalent in java?" Watch "goto equivalent in java?" New topic
Author

goto equivalent in java?

sahar sa
Ranch Hand

Joined: Jul 06, 2009
Posts: 102
Dear friends,
Is there any equivalent for GOTO and LABLE in java? if not is there any other statement or command or keyword which I can use? (I searched but Icouldnt find any clear answer)
I need my program to jump from a method to an specific line in a class! Is that even possible?

Thank you so much,
Sahar.
Chinna Eranna
Ranch Hand

Joined: Dec 08, 2009
Posts: 174

Java does not have goto statement.

There is no need for goto statement. You can achieve what you want to do.. without go to.


- Chinna
Pushkar Choudhary
Rancher

Joined: May 21, 2006
Posts: 425

You can read this.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38509
    
  23
But Java does have a goto keyword. It is there so the compiler can throw better error messages
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38509
    
  23
No, you cannot jump from a method to a "specific line", only to another method which you call.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18764
    
  40

FYI... just because you have a feature doesn't mean that you can use it. For example, C/C++ has goto -- but if you use it, I don't think I have ever been to a "code review" that anyone (that uses it) has gotten out unscathed (and without a request to fix it).

Unless you are going to be programming in an environment where no one will ever see your code, or in assembly language (that lack all the structured constructs), don't use goto (even in languages that have them).



As a side story, once I was really really stuck at a problem -- and out of frustration, churned out the solution using a labelled loop (with break to label) -- a feature that *is* supported in Java. In retrospect, I was definitely tired, and should have just gone home. A week later, I had already moved on to something else, during the code review it came up. And it was quick.... who did this?.... It was me... Serious? ... I'll fix it.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38509
    
  23
Assembler is a different matter altogether. An IF-THEN is implemented something like this
if (x <= y)
{
. . .
}
else
{
. . .
}
and in assembler it comes out something like
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Henry Wong wrote:FYI... just because you have a feature doesn't mean that you can use it. For example, C/C++ has goto -- but if you use it, I don't think I have ever been to a "code review" that anyone (that uses it) has gotten out unscathed (and without a request to fix it).

Sometimes it's still the best solution, though--breaking out of deep loops, for example. It's in embedded programming all the time (along with setjmp/longjmp).
 
 
subject: goto equivalent in java?