Is there a goto statement in Java?

前端 未结 23 1947
醉酒成梦
醉酒成梦 2020-11-22 04:50

I\'m confused about this. Most of us have been told that there isn\'t any goto statement in Java.

But I found that it is one of the keywords in Java. Where can it be

相关标签:
23条回答
  • 2020-11-22 05:41

    See the following link is shows all java reserved words and tells you what versions they where added.

    http://java.sun.com/docs/books/tutorial/java/nutsandbolts/_keywords.html

    goto is reserved, even though it is not currently used, never say never however :)

    0 讨论(0)
  • 2020-11-22 05:44

    Of course it is keyword, but it is not used on level of source code.

    But if you use jasmin or other lower level language, which is transformed to bytecode, then "goto" is there

    0 讨论(0)
  • 2020-11-22 05:45

    As was pointed out, there is no goto in Java, but the keyword was reserved in case Sun felt like adding goto to Java one day. They wanted to be able to add it without breaking too much code, so they reserved the keyword. Note that with Java 5 they added the enum keyword and it did not break that much code either.

    Although Java has no goto, it has some constructs which correspond to some usages of goto, namely being able to break and continue with named loops. Also, finally can be thought of as a kind of twisted goto.

    0 讨论(0)
  • 2020-11-22 05:45

    To prohibit declarations of variables with the same name.

    e.g. int i = 0, goto;

    0 讨论(0)
  • 2020-11-22 05:45

    Because although the Java language doesn't use it, JVM bytecode does.

    0 讨论(0)
  • 2020-11-22 05:46

    They are reserved for future use (see: Java Language Keywords)

    The keywords const and goto are reserved, even though they are not currently used.

    The reason why there is no goto statement in Java can be found in "The Java Language Environment":

    Java has no goto statement. Studies illustrated that goto is (mis)used more often than not simply "because it's there". Eliminating goto led to a simplification of the language--there are no rules about the effects of a goto into the middle of a for statement, for example. Studies on approximately 100,000 lines of C code determined that roughly 90 percent of the goto statements were used purely to obtain the effect of breaking out of nested loops. As mentioned above, multi-level break and continue remove most of the need for goto statements.

    0 讨论(0)
提交回复
热议问题