Hello,
I am taking Java fundamental course and I've came up with a confused character i.e # on assigning variable name on Java lately. I have surfed the internet a lot but all I could find is only oprerators, keyword and reserved words are not allowed. so why is # not allowable on variable name in Java. Example - total # is not allowed as a variable name.
Thanks in advance.

Recommended Answers

All 7 Replies

Member Avatar for 1stDAN

yes, # is not allowed in indentifiers. You may learn more about correctly-spelled identifiers on Click Here.

Thanks for the replay but # is not a reserved word or character. I also tried to refer the link but it still demonstrate the same thing. Please explain it more.
Thanks a lot

There is nothing to explain. # is not used because it is not implemented in JAVA to be used that way.
You could put it in a string , but you cannot use it the way you are explaining.
That character is not accepted everywhere anyway. JAVA is designed to run in all enviroments and the #
has multiple meanings. This is probably the main reason you cannot use it that way.

It can mean :

pound
hash
number

and more. How is JAVA suppose to know what total is by using this character?

Member Avatar for 1stDAN

Sorry, my selected link is wrong, unfortunately I have selected somewhat what deals with the Java persistence query language (so there we have similar rules for creating identifiers but they may slightly differ from Java-language rules).

The correct link for Java language is: Click Here

Thanks for the replay. It really helped me. But why isn't in any part of Java tutorial or books about #?

Member Avatar for 1stDAN

Simply follow my second link (also first link works) to find the information you are seeking for. Letter # is indirectly excluded from to be legal for identifiers in both methods isJavaIdentifierStart(int) and isJavaIdentifierPart(int) of Character class. There you can find the exact rules which letters are allowed for constructing indentifiers. All other letters not mentioned there are not allowed. So # (hash mark) is not mentioned there, hence # is not a valid character.

Btw, this second link leads you to complete famous Java language specification (SE 7).

There is documentation on it if you use the symbol as intended with an @see in JAVADOC

Click Here

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.