1

The keyword "goto" in java is not supported. I have read somewhere once that it is supported in java bytecode, and that there was a obfuscator that used this to create unreadable spaghetti code. However, I cannot find this page again, So I am asking this question here: Does the goto keyword work in compiled form?

user2097804
  • 1,122
  • 4
  • 13
  • 22
  • possible duplicate of [Java compilers or JVM languages that support goto?](http://stackoverflow.com/questions/992930/java-compilers-or-jvm-languages-that-support-goto) – Perception Feb 28 '13 at 01:23
  • No, a workaround is to use throw/catches. This allows you to jump back down. To jump back, put the catch in a loop. – QuentinUK Feb 28 '13 at 01:29
  • 1
    you can use break/continue with labels which is very similar to a (limited) goto. – jtahlborn Feb 28 '13 at 01:33

2 Answers2

3

The Java keyword list specifies the goto keyword, but it is kept reserved. Means it is not used.

If you try to use it will show you error

This was probably done in case it were to be added to a later version of Java.

If goto weren't on the list, and it were added to the language later on, existing code that used the word goto as an identifier (variable name, method name, etcetera) would break. But because goto is a keyword, such code will not even compile in the present, and it remains possible to make it actually do something later on, without breaking existing code.

check this video.. James gosling tell about goto...he used goto when he designed java compiler

antony.trupe
  • 10,640
  • 10
  • 57
  • 84
Pragnani
  • 20,075
  • 6
  • 49
  • 74
  • I know this. But if you potentially had a java compiler that supported the goto keyword, would the jvm be able to run the code? Does the jvm support some kind of goto, even if the compiler doesn't? – user2097804 Feb 28 '13 at 01:23
  • Check my highlighted sentence – Pragnani Feb 28 '13 at 01:24
  • 1
    @user2097804: Java bytecode contains goto, an example can be found in my answer on this question: [Pre and post increment in a for loop](http://stackoverflow.com/questions/15115530/pre-and-post-increment-in-a-for-loop/15115624#15115624) – jlordo Feb 28 '13 at 01:24
  • I have also read somewhere that the guy who created java (forgot his name) had goto in the early versions, but it got removed. Is it possible it was removed from the compiler but not the jvm? – user2097804 Feb 28 '13 at 01:25
  • @user2097804 James Golsing – Pragnani Feb 28 '13 at 01:26
  • @user2097804 hi check this video.. James gosling tell about goto...he used goto when he designed java compiler http://www.youtube.com/watch?v=9ei-rbULWoA&t=17m25s – Pragnani Feb 28 '13 at 01:29
  • @user2097804 now you got 6 – Pragnani Feb 28 '13 at 01:30
2

yes, the "goto" is part of the JVM. Many constructs depend on it (like if / else, while, etc).

It's the equivalent of the "call" in assembly language.

But you're right, it's not available in java source code, only in byte code.

Other jvm-based languages might implement it.

Here is the Jasmin page about it : https://www.vmth.ucdavis.edu/incoming/Jasmin/ref-_goto.html (opcode is 0xA7)

huelbois
  • 6,762
  • 1
  • 19
  • 21