简体   繁体   中英

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.

Read @Heinzi answer

2.2.6 No More Goto Statements

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.

The Java Language Environment, James Gosling and Henry McGilton, 1996

There is no "goto" in java. And "continue" does a little bit other function. You can use "continue" for example in loops like:

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++; 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. 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.

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

Basically, there is no practical way to do that in Java. You appear to be trying to do the equivalent of a "goto", and that is not supported in Java. The break label and continue label statements can only branch to an enclosing labelled statement.

Now according to the Java formal grammar you could write this:

  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 ...)

  • The continue (or a break ) makes the next statement unreachable.

See also: Alternative to a goto statement in Java


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. But the JLS says that the first println will be treated as reachable, so that you won't get an unreachable code error.

I guess this might be useful if you are generating this source code. Apart from that, it is (IMO) just a curiosity. It is simpler to do this with a regular if / else statement ... or by deleting the first "print" entirely.

Theoretically, Java have Jump statements return and break .

The return statement jumps out of a method, with or without returning values to the calling statement.

The break statement jumps out of loops.

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. It existed back in the days of sequential programming.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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