Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

goto equivalent in java?

 
sahar sa
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 174
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java does not have goto statement.

There is no need for goto statement. You can achieve what you want to do.. without go to.
 
Pushkar Choudhary
Rancher
Posts: 425
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can read this.
 
Campbell Ritchie
Sheriff
Posts: 48453
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But Java does have a goto keyword. It is there so the compiler can throw better error messages
 
Campbell Ritchie
Sheriff
Posts: 48453
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, you cannot jump from a method to a "specific line", only to another method which you call.
 
Henry Wong
author
Marshal
Pie
Posts: 20907
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Campbell Ritchie
Sheriff
Posts: 48453
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic