• Post Reply Bookmark Topic Watch Topic
  • New Topic

Legal and Illegal Identifiers  RSS feed

 
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Everybody!
I have one doubt about Legal Identifiers in Java. I was reading some examples of legal and illegal identifiers and i saw:

int e#;

marked as an illegal identifier and i don�t understand why.

Someone could help me??

Thanks,

 
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
According to the Java Language Specification, a Java identifier can contain only A-Z, a-z, 0-9, _ and $, and start only with one of A-Z, a-z, or $.

Seeing as # is not one of those, e# is illegal.
 
Sheriff
Posts: 22845
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's true that # is an illegal character, which answers the specific question. But the part of the JLS you linked to goes on to give an example of an identifier that uses Greek characters, so it isn't true that the only letters that can be used are the Latin letters.

Unfortunately you can't tell from the JLS what the complete list of letters valid in identifiers is, it just refers you to something that could conceivably be implementation-dependent. I find that rather strange.
 
Ranch Hand
Posts: 690
Eclipse IDE Firefox Browser Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Other currency symbols work besides the dollar sign, $.

The character '#'must not be a currency symbol.
 
Lys Marie Santana dos Santos Silva
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks so much for the answer!

I saw the link posted for more references... And there is an example that use Greek characters... But at the explanation, JLS only talks about Latin characters... I think this so much strange too.

 
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Rob Prime:
According to the Java Language Specification, a Java identifier can contain only A-Z, a-z, 0-9, _ and $, and start only with one of A-Z, a-z, or $.

Seeing as # is not one of those, e# is illegal.

Did you follow Rob's link to section 3.8 of the Java Language Specification? What Rob says above is not correct: an identifier does not need to consist only of A-Z, a-z, 0-9 etc. The JLS says that identifiers consist of "Java letters" or digits, and it must not start with a digit.
A "Java letter" is a character for which the method Character.isJavaIdentifierStart(int) returns true. A "Java letter-or-digit" is a character for which the method Character.isJavaIdentifierPart(int) returns true.

The Java letters include uppercase and lowercase ASCII Latin letters A-Z (\u0041-\u005a), and a-z (\u0061-\u007a), and, for historical reasons, the ASCII underscore (_, or \u005f) and dollar sign ($, or \u0024). The $ character should be used only in mechanically generated source code or, rarely, to access preexisting names on legacy systems.

The "Java digits" include the ASCII digits 0-9 (\u0030-\u0039).

Note that this does not say that Java letters are only A-Z, a-z, etc., it says "The Java letters include A-Z, a-z, ...". Greek letters are also Java letters.
[ February 26, 2008: Message edited by: Jesper Young ]
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Correct, I misread (mostly being lazy ).

However, it's better to keep to these characters only, because it improves readability. Everybody in all countries with all text editors should be able to view them, unlike Greek.
 
Paul Clapham
Sheriff
Posts: 22845
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just for interest, my little test program tells me that 45534 out of the 65535 basic Unicode characters can be used as the first character of a Java identifier, and 46377 of them can be used elsewhere in identifiers.
 
Lys Marie Santana dos Santos Silva
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks again!!

I was so confused about this, but now i�m totally cleared!!
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can these characters be used in identifiers? ':' '-' '.'
 
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
swe ka wrote:can these characters be used in identifiers? ':' '-' '.'
No
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And welcome to JavaRanch
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just by accident I realised that i can compile this with no problems:

StringBuilder oStringBuilderºª = new StringBuilder("");

Hence, I should think that 'º' and 'ª' are aso considered "Java letters"?

I hope they don´t expect me to memorize all characters that can be considered as Java letters for the SCJP exam. :-(
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What are those two letters? What is their Unicode value?

Have a look at the Character class; it has methods like isJavaIdentifierPart, and looking at those methods may help work out what a "letter" is or isn't. Also look in your Java installation folder; somewhere there is a file called src.zip or similar. Unzip that, find "java" -> "lang" -> "Character" and then you can see the code of those methods, and see what counts as a "letter".
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are those two letters \u00aa and \u00ba? If so, they would change "2ª" to "segunda" and would be pronounced "a" or "o" by somebody Spanish and therefore count as letters.

And I thereby prove that I know even less Spanish than I know Java .
 
Francisco Montes
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
True. It is exactly like you said: I have a spanish keyboard and these characters are used to go with ordinal numbers. Just like in english 2nd would be 2º (segundo) or 2ª (segunda) depending on the gender of the things being ordered.

I was just a bit surprised at first to see them as "letters" and thus eligible as valid characters for Java identifiers. But it would be like any other language specific symbols (for example, greek characters) so i guess i get the point. :-) Much clearer now!
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Keep in mind that even though it is possible, it is often a bad idea to use non-ASCII characters in code. If someone else wants to read your code and has a more limited text editor he/she will not see all characters for what they are. Even in Strings and chars you should try to use their Unicode equivalents if possible (using \uxxxx).
 
Francisco Montes
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I also agree that using that sort of characters is not going to be the best way to name identifiers. I was just surprised at first that it compiled! ;-)
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But you might want a Toreador2º class if you are Spanish.

Paul Clapham wrote:Just for interest, my little test program tells me that 45534 out of the 65535 basic Unicode characters can be used as the first character of a Java identifier, and 46377 of them can be used elsewhere in identifiers.
And of course you will have to learn all 46377 characters off by heart for your SCJP
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lys Marie Santana dos Santos Silva wrote:Hi Everybody!
I have one doubt about Legal Identifiers in Java. I was reading some examples of legal and illegal identifiers and i saw:

int e#;

marked as an illegal identifier and i don�t understand why.

Someone could help me??

Thanks,



Hi there,

I have the same question and a doubt about if the problem may be the letter 'e' instead of the #?

could e be confused with a digit? eulerian or maybe the e that represents exponential like in 1e-12 ? and maybe that makes it illegal? or I am saying total nonsense?

thank you very much for any info,

BRGDS


 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.

No, the 'e' is certainly not the problem. It has nothing to do with the letter 'e' being used in for example numbers for the exponent. The problem is the '#', which is a character that is not allowed in Java identifiers. You can make a variable named just 'e' without problems, and if you use some other name like 'x#' it's also illegal.

Note there's no need to guess about these things - it's very easy to write a small program and try to compile it to see if the compiler accepts it or not.

 
Gabriel Lichtenstein
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper de Jong wrote:Welcome to the Ranch.

No, the 'e' is certainly not the problem. It has nothing to do with the letter 'e' being used in for example numbers for the exponent. The problem is the '#', which is a character that is not allowed in Java identifiers. You can make a variable named just 'e' without problems, and if you use some other name like 'x#' it's also illegal.

Note there's no need to guess about these things - it's very easy to write a small program and try to compile it to see if the compiler accepts it or not.



Thank you very much for the quick reply Jesper, it makes sense now to me.



 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!