Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why # is not allowed in an identifier?

Java allows identifier to start with or contain $ (dollar sign). e.g int a$b;

But why # is not allowed in an identifier? What is the specific reason? Is # an operator or something in Java? e.g int a#b;

like image 963
Ajinkya Kulkarni Avatar asked May 18 '10 04:05

Ajinkya Kulkarni


People also ask

Why is music so important?

Music improves your health and wellbeing A study from Harvard has shown that relaxing music may lower blood pressure and heart rate after physical exertion. It can also improve mood and reduce anxiety and through bringing people together, can be an antidote to loneliness and social isolation.

Who are you Keith Moon?

Who Are You was the Who's last album to feature Keith Moon as their drummer, who died three weeks after it was released. The ironic nature of the text "Not to Be Taken Away" that was stencilled on Moon's chair on the album cover was noted by some critics.


3 Answers

I'd say that it is a combination of readability and the historical antecedents of the language. Remember that the Java syntax was designed to be easy-on-the-eye for C and C++ programmers. (And as @dan04 points out, the # character is significant in most dialects of C and C++.)

Incidentally, while $ is technical legal in Java identifiers, it is reserved for use by compilers, code generators and other things. If you use $ in identifiers in your source code, you risk getting into trouble with collisions with synthetic identifiers produced by (for example) the javac compiler.

like image 66
Stephen C Avatar answered Oct 06 '22 06:10

Stephen C


I doubt there was a specific reason for disallowing # as much as there was a general aversion to punctuation in identifiers (which could look like operators). Special exceptions were made in order to deal with WORD_SEPARATION and Inner$Classes.

In C and C++, on which Java's syntax is based, # is used for preprocessor directives.

like image 43
dan04 Avatar answered Oct 06 '22 06:10

dan04


Variables can contain letters, digits, underscores and dollar signs - excluding #, @, ~ ,`

I guess this is for the sake of readability - int s#@t; seems odd.

like image 32
Bozho Avatar answered Oct 06 '22 05:10

Bozho