• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Illegal Identifiers in Java

 
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 143
Android Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Rancher
Posts: 1044
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Lakshan Dissanayake
Ranch Hand
Posts: 143
Android Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
If you believe you can tell me what to think, I believe I can tell you where to go. Go read this tiny ad!
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic