Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Illegal Identifiers in Java

 
Gihan Madushanka
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11021
12
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Gihan Madushanka
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15214
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Gihan Madushanka
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15214
36
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 135
Android Java PHP
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Gihan Madushanka
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64700
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ivan Jozsef Balazs
Rancher
Posts: 972
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 135
Android Java PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic