Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 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?

like image 490
klonq Avatar asked Sep 14 '11 08:09

klonq


People also ask

What is double underscore in Java?

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.

What are underscores used for in Java?

Core Java bootcamp program with Hands on practice In earlier versions of Java, the underscore ("_") has used as an identifier or to create a variable name. Since Java 9, the underscore character is a reserved keyword and can't be used as an identifier or variable name.

Why we use double underscore symbol before the attribute in Python?

A double underscore prefix causes the Python interpreter to rewrite the attribute name in order to avoid naming conflicts in subclasses. This is also called name mangling—the interpreter changes the name of the variable in a way that makes it harder to create collisions when the class is extended later.

What does _ and __ mean in Python?

The use of double underscore ( __ ) in front of a name (specifically a method name) is not a convention; it has a specific meaning to the interpreter. Python mangles these names and it is used to avoid name clashes with names defined by subclasses.


3 Answers

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
like image 74
Andreas Dolk Avatar answered Sep 18 '22 14:09

Andreas Dolk


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.

like image 31
Joachim Sauer Avatar answered Sep 17 '22 14:09

Joachim Sauer


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;}
like image 24
Bitmap Avatar answered Sep 20 '22 14:09

Bitmap