• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

goto equivalent in java?

 
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 174
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Rancher
Posts: 425
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can read this.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, you cannot jump from a method to a "specific line", only to another method which you call.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic