开发者

Java what is the double underscore notation for?

I was browsing the web and found this example. Within the public static void main method is this line syntax which I have never seen before __main:

As in:

public class Klass {
    public static void main(String[] args) {
        // code goes here...
__main:
        // and mode code here...
    }
}

I tried punching this into eclipse and got a little yellow underline with a tool-tip which said "The label __main is never explicitly referenced".

I can't find开发者_JS百科 anything online and I'm really curious! I've been programming in Java for years and I've never seen this syntax before. What does it do, why would you use it, where can I learn more?


That's a label. You can use it with the break and continue statements. It's not necessary, to use the double underscore.


To be more precise: it is (the first) part of a Labeled Statement (JLS 14.7):

Identifier : Statement


Double underscores in Java have not semantic meaning (the same way that single underscores don't have any semantic meaning).

The only meaning they could have would depend on some naming convention that defines what they are used for.

It looks like the code you look at uses double underscores for labels. Since labels are not frequently used in Java, I wouldn't worry to much about it.


A goto statement in C and its counterparts are accompanied by a Label argument. In a defined method, a goto label; statement will fire the routine proceeding the label. Below is an example demonstrated by Greg Rogers in this Post.

void foo()
{
    if (!doA())
        goto exit;
    if (!doB())
        goto cleanupA;
    if (!doC())
        goto cleanupB;

    // everything succeed
    return;

cleanupB:
    undoB();
cleanupA:
    undoA();
exit:
    return;
}

A goto can be a very effective tool in Java, but Java does not explicitly support the goto keyword even though the keyword has been reserved by the language. Using a break statement will enable the command jump out of the label to proceed to the statement after the label.

Example:

public class Klass {
  public static void main(String[] args) {
  // code goes here...
  __main:
        {
         if(args.length==0)
         {
          break __main;
         }
        }
  //code after label
    }
}

The package com.sun.org.apache.bcel.internal.generic.GOTO, I have not used it personally but I think it can aid to achieve the same code structure as demonstrated by Greg Rogers like this:

 void foo()
  {
      if (!doA())
          GOTO exit;
      if (!doB())
          GOTO cleanupA;
      if (!doC())
          GOTO cleanupB;

      // everything succeed
      return;

  cleanupB:
      undoB();
  cleanupA:
      undoA();
  exit:
      return;
  }

  void undoB(){}
  void undoA(){}
  boolean doC(){return false;}
  boolean doA(){return false;}
  boolean doB(){return false;}


The double underscores are irrelevant. You are really asking about Labels. Here is some info . Basically they are used for controling breaks. Also a fun trivia question in interviews: Does the following code compile?

public void doSomething(){
   http://www.stackoverflow.com
}

Why yes it does and now you know why:D


Java has labels (but no goto). The double underscore has no meaning but it is a legal Java identifier.

From the JLS:

14.7 Labeled Statements Statements may have label prefixes.

LabeledStatement:
  Identifier : Statement

LabeledStatementNoShortIf:
  Identifier : StatementNoShortIf

The Identifier is declared to be the label of the immediately contained Statement.

Unlike C and C++, the Java programming language has no goto statement; identifier statement labels are used with break (§14.14) or continue (§14.15) statements appearing anywhere within the labeled statement.

The scope of a label declared by a labeled statement is the statement immediately enclosed by the labeled statement.


It's just a label. you could write bla:, for the same effect.

Labels are generally used with goto statements, but as far as I know, goto is forbidden in java (and greatly discouraged in any other high-level language).

This can also, however, be used inside loops with continue and break, e.g. continue someLabel;, if you want the continue or the break to refer to an outer loop rather than the immediate one you are in right now.


double underscore and labels tend to be used more in C. Perhaps the developer have been writing C recently.

BTW: Labels are rarely used for loops but an even rarer use is to jump out of scope.

scope: {
  // some code
  if (flag)
      break scope; // no loop
  // some more code
}

Because labels are rare, they can more confusing than useful. ;)


Nobody seems to have answered EXACTLY what you actually asked (but the "goto" answers are probably what you were really looking for) and they are right about it just being an identifier, but not why they were used in this case.

The double-underscore is typically used within the system to prevent possible collisions with other identifiers... usually it is used in cases where the scope is available to the user. In this case without the underscores you might be prevented from making a "main" variable at some point or another, or perhaps some system-class would be prevented from using the word main.

In this case I'm guessing that if the variable had been named "main" it would conflict with the "public static void main" being defined.

I guess in summary the double-underscore is a form of manual scoping..

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜