0

I'm readind an eBook that sais:

int :b;
int e#;

are not legal identifiers, but I don't understand why ":" and "#" are not legal tokens. Do you have any idea?

bouhmid_tun
  • 344
  • 1
  • 4
  • 17

3 Answers3

7

Because the Java Language Specification says so:

An identifier is an unlimited-length sequence of Java letters and Java digits, the first of which must be a Java letter.

Identifier:
    IdentifierChars but not a Keyword or BooleanLiteral or NullLiteral

IdentifierChars:
    JavaLetter
    IdentifierChars JavaLetterOrDigit

JavaLetter:
    any Unicode character that is a Java letter (see below)

JavaLetterOrDigit:
    any Unicode character that is a Java letter-or-digit (see below)

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 pre-existing names on legacy systems.

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

Letters and digits may be drawn from the entire Unicode character set, which supports most writing scripts in use in the world today, including the large sets for Chinese, Japanese, and Korean. This allows programmers to use identifiers in their programs that are written in their native languages.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
1

Valid identifiers are defined in the JLS #3.8. In particular:

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.

Those two lines output false, which makes your two identifiers invalid:

System.out.println(Character.isJavaIdentifierPart(':'));
System.out.println(Character.isJavaIdentifierPart('#'));

Note that valid identifiers include A-Z, a-z, _, $ but also many "exotic" characters. For example, this is a valid identifier:

int a_£à胥;
Community
  • 1
  • 1
assylias
  • 321,522
  • 82
  • 660
  • 783
0

From the JLS:

An identifier is an unlimited-length sequence of Java letters and Java digits, 
the first of which must be a Java letter. 

..

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 pre-existing names on legacy systems. 
dimitrisli
  • 20,895
  • 12
  • 59
  • 63