简体   繁体   English

Java - 下划线

[英]Java - underscore

Don't know if it's a duplicate (couldn't find the words to search like "java character allowed" ). 不知道它是否重复(找不到像“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) (我尝试使用我的IDE,我很惊讶它是编译好的)

The underscore character is treated just like a letter in Java, as far as identifiers are concerned. 就标识符而言,下划线字符被视为Java中的字母。 The JLS, Section 3.8 covers what an identifier can consist of: JLS第3.8节涵盖了标识符可以包含的内容:

An identifier is an unlimited-length sequence of Java letters and Java digits, the first of which must be a Java letter. 标识符是无限长度的Java字母和Java数字序列,第一个必须是Java字母。

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 \$). “Java字母”包括大写和小写ASCII拉丁字母AZ(\\ u0041- \\ u005a)和az(\\ u0061- \\ u007a),并且由于历史原因,ASCII下划线(_,或\\ u005f)和美元符号($,或\\ u0024)。 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 . 它定义了一个名为_的类,其中一个成员变量具有相同的类名_称为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. 有3个构造函数 - 一个没有参数,什么都不做,一个有一个类型_ f参数,一个有两个参数fg ,类型_什么都不做。

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 ). 第二个构造函数声明一个类型为_的局部变量t并将参数f赋给它,然后将t赋值给f (它不触及实例变量f )。

Identifiers in Java may contain any Unicode character, unless the identifier is neither a keyword or a bool or a null literal. Java中的标识符可以包含任何Unicode字符,除非标识符既不是关键字,也不是bool或null文字。 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 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). 你看它以class _开头,所以我们声明一个名为(下划线字符)的类。 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. 总而言之,它编译是因为它是有效的java代码,因为我试图在上面解释。

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!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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