简体   繁体   English

如何跳入Java

[英]How to jump in java

I have a code like this 我有这样的代码

public class Test
{

    public static void main(String[] args)
    {
        continue s;
        System.out.println("I am not supposed to print this");
        s:
        System.out.println("I am suppose to print this");

    }
}

I get the error 我得到错误

java: undefined label: s

What is wrong ? 怎么了 ?

Jumping like this is not possible in Java, only way to jump is from loops, while and do. 在Java中不可能像这样跳跃,唯一的跳跃方式是从循环,while和do。

Read @Heinzi answer 阅读@Heinzi的答案

2.2.6 No More Goto Statements 2.2.6没有更多的Goto语句

Java has no goto statement . Java没有goto语句 Studies illustrated that goto is (mis)used more often than not simply "because it's there". 研究表明,goto被(误用)的原因不只是“因为它存在”而被更多地使用。 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. 取消goto导致语言的简化-例如,在for语句的中间没有关于goto效果的规则。 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. 对大约100,000行C代码的研究确定,大约90%的goto语句仅用于获得摆脱嵌套循环的效果。 As mentioned above, multi-level break and continue remove most of the need for goto statements. 如上所述,多级中断并继续删除了对goto语句的大部分需求。

The Java Language Environment, James Gosling and Henry McGilton, 1996 Java语言环境,James Gosling和Henry McGilton,1996年

There is no "goto" in java. Java中没有“ goto”。 And "continue" does a little bit other function. 而“继续”还有其他功能。 You can use "continue" for example in loops like: 您可以在循环中使用“ continue”,例如:

class ContinueDemo {
    public static void main(String[] args) {

        String searchMe = "peter piper picked a " + "peck of pickled peppers";
        int max = searchMe.length();
        int numPs = 0;

        for (int i = 0; i < max; i++) {
            // interested only in p's
            if (searchMe.charAt(i) != 'p')
                continue;

            // process p's
            numPs++;
        }
        System.out.println("Found " + numPs + " p's in the string.");
    }
}

In the example above, if for example searchMe.charAt(5) != 'p' then the loop will continue from the beginning of loop from i=6, and numPs++; 在上面的示例中,例如,如果searchMe.charAt(5) != 'p'则循环将从i = 6和numPs++;的循环开始处继续numPs++; will not be processed. 将不会被处理。

You can read more about this here: Branching Statements 您可以在此处阅读有关此内容的更多信息: 分支语句

continue is a keyword in Java used to skip iterations of a loop. continue是Java中的关键字,用于跳过循环的迭代。 If you are trying to find an equivalent to GOTO , you should reconsidering how you are trying to solve your problem, GOTO is never a valid option, ever. 如果您试图找到与GOTO等效的产品,则应重新考虑如何解决问题, GOTO永远不是有效的选择。

据我所知,Java中没有goto(有一个关键字,但没有任何意义)

Basically, there is no practical way to do that in Java. 基本上,在Java中没有实际的方法可以做到这一点。 You appear to be trying to do the equivalent of a "goto", and that is not supported in Java. 您似乎正在尝试执行等效的“ goto”操作,而Java不支持该功能。 The break label and continue label statements can only branch to an enclosing labelled statement. break labelcontinue label语句只能分支到封闭的带标签语句。

Now according to the Java formal grammar you could write this: 现在,根据Java正式语法,您可以编写以下代码:

  s: {
      continue s;
      System.out.println("I am not supposed to print this");
  }
  System.out.println("I am suppose to print this");

but that still won't compile for two reasons: 但这仍然无法编译,原因有二:

  • The continue is only allowed to branch to a label on a loop statement. 仅允许继续分支到循环语句上的标签。 (A break doesn't have that restriction ... but ...) break没有那个限制...但是...)

  • The continue (or a break ) makes the next statement unreachable. continue (或break )使下一条语句不可访问。

See also: Alternative to a goto statement in Java 另请参见: Java中goto语句的替代方法


But there is one rather tricky way to get your code to "work": 但是有一种相当棘手的方法可以使您的代码“起作用”:

  static final boolean flag = true;  // class attribute ...

  ...

  s: {
      if (flag) break s;
      System.out.println("I am not supposed to print this");
  }
  System.out.println("I am suppose to print this");

The "test" there will be evaluated by the compiler so that the break is effectively unconditional. 编译器将对“测试”进行评估,以使break实际上是无条件的。 But the JLS says that the first println will be treated as reachable, so that you won't get an unreachable code error. 但是JLS表示,第一个println将被视为可访问的,因此您不会遇到无法访问的代码错误。

I guess this might be useful if you are generating this source code. 我想如果您生成此源代码,这可能会很有用。 Apart from that, it is (IMO) just a curiosity. 除此之外,这只是(IMO)的好奇心。 It is simpler to do this with a regular if / else statement ... or by deleting the first "print" entirely. 使用常规的if / else语句...或通过完全删除第一个“打印”来进行此操作更为简单。

Theoretically, Java have Jump statements return and break . 从理论上讲,Java具有Jump语句returnbreak

The return statement jumps out of a method, with or without returning values to the calling statement. return语句会跳出一个方法,无论是否将值返回给调用语句。

The break statement jumps out of loops. break语句跳出循环。

As mentioned in the earlier answers, goto is not available in Java, and is not considered to be a good programming practice in procedural or object oriented programming. 正如前面的答案中提到的那样, goto在Java中不可用,并且不被视为过程式或面向对象编程中的良好编程习惯。 It existed back in the days of sequential programming. 它存在于顺序编程时代。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM