my dog learned polymorphism
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Valid Identifiers. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Valid Identifiers." Watch "Valid Identifiers." New topic

Valid Identifiers.

Anthony Roy

Joined: Oct 08, 2003
Posts: 8
I have seen quite a few threads on this topic. The Java Language Specification notes that Identifiers cannot match any keyword, true, fals or null. In addition, the following requirements are enforced:
Starting character: Must be a Java Letter.
Other Characters: Must be a Java Letter or a Java Digit.
The definitions of Java Letter and Java Digit are suitably vague! It seems that the only sure way of knowing is by using the Character.isJavaIdentifierStart(char c) and Character.isJavaIdentifierPart(char c) methods.
On my (UK) English system, this equates to JL: [a-zA-Z_$�]; JD: [0-9]
The following program may aid those with international keyboards:
import java.util.TreeSet;
public class JavaIdentifiers{
public static void main(String[] args){

String chars = (args.length > 0) ? args[0] :
char[] charArray = chars.toCharArray();
TreeSet javaLetters = new TreeSet();
TreeSet javaDigits = new TreeSet();
TreeSet other = new TreeSet();

for (int i=0; i<charArray.length; i++){
char current = charArray[i];
if ( Character.isJavaIdentifierStart(current) ) {
javaLetters.add(new Character(current));
} else if ( Character.isJavaIdentifierPart(current) ){
javaDigits.add(new Character(current));
} else {
other.add(new Character(current));
System.out.println("Allowed in Identifiers:");
for (Iterator it = javaLetters.iterator(); it.hasNext(); ){

System.out.println("Allowed in Identifiers (but not as first character):");
for (Iterator it = javaDigits.iterator(); it.hasNext(); ){

System.out.println("Not Allowed in Identifiers:");
for (Iterator it = other.iterator(); it.hasNext(); ){

Ant...<br /><a href="" target="_blank" rel="nofollow"></a>
Vad Fogel
Ranch Hand

Joined: Aug 25, 2003
Posts: 504
Hi Anthony, good stuff. You have to or to compile.
[ October 08, 2003: Message edited by: Vad Fogel ]
Anthony Roy

Joined: Oct 08, 2003
Posts: 8
Whoops! Cut and paste error I'm afraid...
I agree. Here's the link:
subject: Valid Identifiers.
It's not a secret anymore!