wood burning stoves 2.0*
The moose likes Java in General and the fly likes Illegal Identifiers in Java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Illegal Identifiers in Java" Watch "Illegal Identifiers in Java" New topic
Author

Illegal Identifiers in Java

Gihan Madushanka
Ranch Hand

Joined: Apr 09, 2013
Posts: 71

1. int :b;
2. int -d;
3. int e#;
4. int .f;
5. int 7g;


In java an identifier cannot start with a number. Therefore 3rd(in math e is a constant ) & 5th ones are obviously illegal identifiers. But what about 1st,2nd and 4th identifiers. The book that I am currently going through says that they are illegal identifiers. They start with punctuation marks. Does not Java allow identifiers to be started with punctuation marks ?
Is it a must to create identifiers which adhere to those standards or is it a good programming practice ?


Thanks in advance
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Gihan Madushanka wrote:Does not Java allow identifiers to be started with punctuation marks ?


You can answer that yourself by checking in the relevant section of the JLS, or by just trying to compile the code.

Is it a must to create identifiers which adhere to those standards or is it a good programming practice ?


Obviously it's a must to adhere to the rules spelled out in the JLS and enforced by the compiler. By definition.

Any further naming conventions are not a must as far as Java is concerned, but might be a must as far as your isntructor or boss is concerned.
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10167
    
    8

Jeff Verdegan wrote:
Any further naming conventions are not a must as far as Java is concerned, but might be a must as far as your isntructor or boss is concerned.

...and team mates. When I find code with names like s1,s2,s3,s3 it makes me see red


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Gihan Madushanka
Ranch Hand

Joined: Apr 09, 2013
Posts: 71
Maneesh Godbole wrote:
Jeff Verdegan wrote:
Any further naming conventions are not a must as far as Java is concerned, but might be a must as far as your isntructor or boss is concerned.

...and team mates. When I find code with names like s1,s2,s3,s3 it makes me see red

Thanks Maneesh!!!
Any idea about punctuation marks ?
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Gihan Madushanka wrote:Any idea about punctuation marks ?


As already stated:

You can answer that yourself by checking in the relevant section of the JLS, or by just trying to compile the code.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

Gihan Madushanka wrote:In java an identifier cannot start with a number. Therefore 3rd(in math e is a constant ) & 5th ones are obviously illegal identifiers.

That's incorrect.

Just because in math, the letter e is being used to indicate a numerical constant, doesn't mean that e is a number in Java. So, that is not the reason why the 3rd is not allowed. If this were true, that would mean you couldn't have variables of which the name starts with the letter e. That would be madness!

Also, the specification says that identifiers can't start with a digit (that's not the same as a number). A digit is 0, 1, 2, 3, 4, 5, 6, 7, 8, 9.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Gihan Madushanka
Ranch Hand

Joined: Apr 09, 2013
Posts: 71
Jesper de Jong wrote:
Gihan Madushanka wrote:In java an identifier cannot start with a number. Therefore 3rd(in math e is a constant ) & 5th ones are obviously illegal identifiers.

That's incorrect.

Just because in math, the letter e is being used to indicate a numerical constant, doesn't mean that e is a number in Java. So, that is not the reason why the 3rd is not allowed. If this were true, that would mean you couldn't have variables of which the name starts with the letter e. That would be madness!

Is there any possible reason for 3rd one to be illegal?
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Gihan Madushanka wrote:Is there any possible reason for 3rd one to be illegal?


You can answer that yourself by checking in the relevant section of the JLS.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

Gihan Madushanka wrote:Is there any possible reason for 3rd one to be illegal?

The 3rd one is illegal because it has # in it, which is not a valid character in an identifier.
Gihan Madushanka
Ranch Hand

Joined: Apr 09, 2013
Posts: 71
Jesper de Jong wrote:
Gihan Madushanka wrote:Is there any possible reason for 3rd one to be illegal?

The 3rd one is illegal because it has # in it, which is not a valid character in an identifier.

Yes you are correct. I created a variable called e45 and it was compiled successfully. When I changed it to e# the compiler indicated that # is an illegal symbol.
Thanks for your advice.
Cheers!!!
Gihan Madushanka
Ranch Hand

Joined: Apr 09, 2013
Posts: 71
Jeff Verdegan wrote:
Gihan Madushanka wrote:Is there any possible reason for 3rd one to be illegal?


You can answer that yourself by checking in the relevant section of the JLS.

Thanks
Supun Lakshan Dissanayake
Ranch Hand

Joined: Oct 26, 2012
Posts: 132

valid indentifiers should ONLY contains a to z, A to Z, 0 to 9, $(dollar sign), _(underscore) and
valid indentifier MUST begin with non digit character.

hope this was helpful


Are you better than me? Then please show me my mistakes..
Gihan Madushanka
Ranch Hand

Joined: Apr 09, 2013
Posts: 71
Supun Lakshan Dissanayake wrote:valid indentifiers should ONLY contains a to z, A to Z, 0 to 9, $(dollar sign), _(underscore) and
valid indentifier MUST begin with non digit character.

hope this was helpful

Thanks Supun
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60739
    
  65

Maneesh Godbole wrote:When I find code with names like s1,s2,s3,s3 it makes me see red

If it were from a member of my team, that member might see pink, if you get my drift.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Ivan Jozsef Balazs
Rancher

Joined: May 22, 2012
Posts: 866
    
    5
Supun Lakshan Dissanayake wrote:valid indentifiers should ONLY contain a to z, A to Z, 0 to 9, $(dollar sign), _(underscore)


Nope. Valid idenitifers can contain JavaLetters,

JavaLetter:
any Unicode character that is a Java letter

A "Java letter" is a character for which the method Character.isJavaIdentifierStart(int) returns true.

For example accented letters like these ones: áéíóúöüőű. Our Chinese, Japanese, Korean etc. friends might also add some examples ;-)

Having begun as a Cobol programmer in the good old Cobol days, using English procedure and variable names became very natural to me, and I do not use "national language" class, method or variable names, and I do not forcibly suggest their usage's being a good idea, but hélas, they are allowed.
Supun Lakshan Dissanayake
Ranch Hand

Joined: Oct 26, 2012
Posts: 132

Ivan Jozsef Balazs wrote:
Supun Lakshan Dissanayake wrote:valid indentifiers should ONLY contain a to z, A to Z, 0 to 9, $(dollar sign), _(underscore)


Nope. Valid idenitifers can contain JavaLetters,

JavaLetter:
any Unicode character that is a Java letter


yeah, I almost forget it. Thanks Ivan Jozsef Balazs!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Illegal Identifiers in Java
 
Similar Threads
Legal and Illegal Identifiers
Can '#' be used in an identifier?
Illegal Identifiers
Queries related to K&B SCJP 6 book
Questions on naming convention