简体   繁体   中英

Java - underscore

Don't know if it's a duplicate (couldn't find the words to search like "java character allowed" ). I had this question on test interview :


Consider the following class :

 class _ {_ f; _(){}_(_ f){_ t = f; f = t;}_(_ f, _ g){}} 
  1. Does this compile ?
  2. If yes, what does this code do ?

So my answer was no, but I had wrong. Could someone explain me how does this compile ? (I try on my IDE and I was surprise that yes its compiles fine)

The underscore character is treated just like a letter in Java, as far as identifiers are concerned. The JLS, Section 3.8 covers what an identifier can consist of:

An identifier is an unlimited-length sequence of Java letters and Java digits, the first of which must be a Java letter.

and

The "Java letters" include uppercase and lowercase ASCII Latin letters AZ (\A-\Z), and az (\a-\z), and, for historical reasons, the ASCII underscore (_, or \_) and dollar sign ($, or \$). The $ character should be used only in mechanically generated source code or, rarely, to access pre-existing names on legacy systems.

So this compiles. It defines a class called _ , with a member variable of the same class name _ called f . There are 3 constructors -- one with no arguments that does nothing, one with one f argument of type _ , and one with two arguments f and g of type _ that does nothing.

That second constructor declares a local variable t of type _ and assigns the parameter f to it, then assigns t back to f (it doesn't touch the instance variable f ).

Identifiers in Java may contain any Unicode character, unless the identifier is neither a keyword or a bool or a null literal. And they should not start with a digit. For details, check out the reference:
http://docs.oracle.com/javase/specs/jls/se7/html/jls-3.html#jls-3.8

Your example code satisfies these criteria. The code after a little renaming is as follows:

class SomeClass
{
    SomeClass f;
    SomeClass() {}
    SomeClass(SomeClass f)
    {
        SomeClass t = f;
        f = t;
    }
    SomeClass(SomeClass f, SomeClass g) {}
}

You see it starts with class _ , so we are declaring a class named (the underscore character). Perhaps this becomes more sensible if you replace _ with a word you might use to name a class.

class thing {  //we're declaring a class called 'thing'

thing f;  //the class contains a member variable called 'f' that is of type thing

thing(){  //this is the constructor
}

//It was a default constructor, it doesn't do anything else

//Here is another constructor

thing(thing f) {   //This constructor takes a parameter named 'f' of type thing

// We are declaring a local variable 't' of type thing, and assigning the value of f that was passed in when the constructor was called
thing t = f;  

f = t;  //Now we assign the value of t to f, kind of an odd thing to do but it is valid.
}

//Now we have another constructor
thing(thing f, thing g){  //This one takes two parameters of type thing called 'f' and 'g'
}  //And it doesn't do anything with them...

} //end class declaration

So in summary, it compiles because it is valid java code as I tried to explain above.

That's really cool!

class _ //Underscore is a valid name for a class
{
    _ f; //A reference to another instance of the _ class named f

    _() //The constructor of the _ class
    {
    //It's empty!
    }

    _(_ f) //A second constructor that takes an other instance of _ called f as a parameter
    {    
        _ t = f; // A new reference to a _ object called t that now points to f
        f = t;   // f points to t (which, well, points to f :) )
    }

    _(_ f, _ g) //A third constructor with two parameters this time of _ instances.
    {
        //It's empty!
    }
} // End of class definition!

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