Is there a goto statement in Java?

前端 未结 23 2417
醉酒成梦
醉酒成梦 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: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;
        }
    }
    

提交回复
热议问题