What is significance of putting “$” sign before a parameter in Java / Android?

后端 未结 5 1702
一生所求
一生所求 2021-01-20 23:29

What is the difference or significance of putting $ sign before any variable or parameter..

e.g.

Suppose here is my class...

 public class Vecto         


        
相关标签:
5条回答
  • 2021-01-20 23:40

    While many people have pointed out that $ is legal in identifiers, I refute the use of $ (at least as an initial token) as it goes against the Java Naming Conventions:

    Variable names should not start with underscore _ or dollar sign $ characters, even though both are allowed.

    While this does not include the case of foo$bar I follow the JLS recommendation that the $ be used only in generated code. The Java compiler uses $ when it generates anonymous classes, for instance.

    If nothing else, I would consider your code ugly :-)

    0 讨论(0)
  • 2021-01-20 23:40

    Variable names are case-sensitive. A variable's name can be any legal identifier — an unlimited-length sequence of Unicode letters and digits, beginning with a letter, the dollar sign "$", or the underscore character "". The convention, however, is to always begin your variable names with a letter, not "$" or "". Additionally, the dollar sign character, by convention, is never used at all. You may find some situations where auto-generated names will contain the dollar sign, but your variable names should always avoid using it. A similar convention exists for the underscore character; while it's technically legal to begin your variable's name with "_", this practice is discouraged. White space is not permitted.

    Taken from : ttp://download.oracle.com/javase/tutorial/java/nutsandbolts/variables.html#naming

    0 讨论(0)
  • 2021-01-20 23:56

    I don't believe it is anything more than a valid symbol for use in Java variable names. From the Java tutorial on variables ( http://download.oracle.com/javase/tutorial/java/nutsandbolts/variables.html ):

    Variable names are case-sensitive. A variable's name can be any legal identifier — an unlimited-length sequence of Unicode letters and digits, beginning with a letter, the dollar sign "$", or the underscore character "_". The convention, however, is to always begin your variable names with a letter, not "$" or "_". Additionally, the dollar sign character, by convention, is never used at all. You may find some situations where auto-generated names will contain the dollar sign, but your variable names should always avoid using it. A similar convention exists for the underscore character; while it's technically legal to begin your variable's name with "_", this practice is discouraged. White space is not permitted.

    0 讨论(0)
  • 2021-01-20 23:58

    According to the Java Language Specification, it's permissible, but has no special meaning. Note that the specification goes on to recommend that it "only be used in mechanically generated source code, or rarely, to access pre-existing names in a legacy system".

    0 讨论(0)
  • 2021-01-21 00:02

    compiles and runs. kid you not.

    package $;
    
    public class $
    {
        int $;
    
        int $(int $){ return $; }
        void $$(int $){ this.$=$; }
    
        $ _(){ return this; }
    
        $ _;
    
        {
            $$($($));
    
            _._()._._()._();
        }
    }
    
    0 讨论(0)
提交回复
热议问题