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?

1
  • You should have to read Java Language Specification. Commented Sep 10, 2012 at 12:19

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

Comments

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_£à胥; 

1 Comment

Yep. First time I saw greek letters being used as variables in a math function, I did a double-take.
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. 

1 Comment

Java letters include those, but there's plenty of other valid characters.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.