Is there a goto statement in Java?

前端 未结 23 1944
醉酒成梦
醉酒成梦 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:31

    No, goto is not used in Java, despite being a reserved word. The same is true for const. Both of these are used in C++, which is probably the reason why they're reserved; the intention was probably to avoid confusing C++ programmers migrating to Java, and perhaps also to keep the option of using them in later revisions of Java.

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

    Because it's not supported and why would you want a goto keyword that did nothing or a variable named goto?

    Although you can use break label; and continue label; statements to effectively do what goto does. But I wouldn't recommend it.

    public static void main(String [] args) {
    
         boolean t = true;
    
         first: {
            second: {
               third: {
                   System.out.println("Before the break");
    
                   if (t) {
                      break second;
                   }
    
                   System.out.println("Not executed");
               }
    
               System.out.println("Not executed - end of second block");
            }
    
            System.out.println("End of third block");
         }
    }
    
    0 讨论(0)
  • 2020-11-22 05:35

    The Java keyword list specifies the goto keyword, but it is marked as "not used".

    It was in the original JVM (see answer by @VitaliiFedorenko), but then removed. It was probably kept as a reserved keyword in case it were to be added to a later version of Java.

    If goto was not on the list, and it gets added to the language later on, existing code that used the word goto as an identifier (variable name, method name, etc...) 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.

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

    No, goto is not used, but you can define labels and leave a loop up to the label. You can use break or continue followed by the label. So you can jump out more than one loop level. Have a look at the tutorial.

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

    It's very much considered one of those things you Do Not Do, but was probably listed as a reserved word to avoid confusion for developers.

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

    The keyword exists, but it is not implemented.

    The only good reason to use goto that I can think of is this:

    for (int i = 0; i < MAX_I; i++) {
        for (int j = 0; j < MAX_J; j++) {
            // do stuff
            goto outsideloops; // to break out of both loops
        }
    }
    outsideloops:
    

    In Java you can do this like this:

    loops:
    for (int i = 0; i < MAX_I; i++) {
        for (int j = 0; j < MAX_J; j++) {
            // do stuff
            break loops;
        }
    }
    
    0 讨论(0)
提交回复
热议问题