What's the point of using labeled statements in Java? [duplicate]

末鹿安然 提交于 2019-12-18 04:34:16

问题


I'm busy studying for my certification and I stumbled upon a concept I've never even heard before - "Labeled Statements". e.g:

'label' : 'statement'

L1: while(i < 0){
     L2: System.out.println(i);
}

So my question is.. why? How is this useful and when would one want to use something like this?


回答1:


The only use that I'm aware of is that you can use labels in break or continue statements. So if you have nested loops, it's a way to break out of more than one level at a time:

OUTER: for (x : xList) {
          for (y : yList) {
              // Do something, then:
              if (x > y) {
                  // This goes to the next iteration of x, whereas a standard
                  // "continue" would go to the next iteration of y
                  continue OUTER;
              }
          }
       }

As the example implies, it's occasionally useful if you're iterating over two things at once in a nested fashion (e.g. searching for matches) and want to continue - or if you're doing normal iteration, but for some reason want to put a break/continue in a nested for loop.

I tend to only use them once every few years, though. There's a chicken-and-egg in that they can be hard to understand because they're a rarely-used construct, so I'll avoid using labels if the code can be clearly written in another way.




回答2:


It can be used to avoid the need for a "not found" flag.

FOUND: {
  for(Type t: list)
     if (t.isTrue())
        break FOUND;

  // handle not found.
}

This is perhaps a misuse of labels, but you can use them to break without a loop.

LABEL: {
    if(condition)
       break LABEL;
    // do something
}

It can also be used to confuse people, which is a good reason to avoid it. ;)

http://www.google.com
while(true) break http;



回答3:


I used to use those as comment statements :) Jokes aside, it is like the Go to statements in basic, which allows you to jump to a line of code, ie during a deep looping structure...

Usage:

scan: {
      int c;
      for (firstUpper = 0 ;
          firstUpper < count ;
          firstUpper += Character.charCount(c)) {
        c = codePointAt(firstUpper);
        if (c != Character.toLowerCase(c)) {
          break scan;
        }
      }
      return this;
    }



回答4:


Here is an example of inordinate break that is likely to be missed out by the rest of the replies. It allows to break a loop within switch{} statement:

loop: for(;;){
  int c=in.read();
  switch(c){
  case -1:
  case '\n':
    break loop;
  case 'a':
    processACommand();
    break;
  case ...
  default:
    break;
  }

}



回答5:


I think that they are required so that you can write Fortran while pretending to write Java. Without them, the aphorism Real programmers write in Fortran whatever language they are using might be invalidated.




回答6:


As other answers have stated, labels are a seldom used part of the Java language.

But in your case some other things should be considered:

  • The labels are quite "generic" and are in fact line numbers: L1, L2, ...

  • The labels are not used in the code.

  • You are studying material for a certification.

This means, that the L1, L2 labels are simply line numbers. I assume, that the text explaining the code refers to that line numbers. In the same way some books and papers enumerate all mathematical terms just for referencing them in the text or to make citations easier.



来源:https://stackoverflow.com/questions/12070942/whats-the-point-of-using-labeled-statements-in-java

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!