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] :
"1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM"+
"`��\\|!\"�$%^&*()-_=+[]{};:'@#~,.<>/?";
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.print(it.next().toString());
}
System.out.println("");
System.out.println("Allowed in Identifiers (but not as first character):");
for (Iterator it = javaDigits.iterator(); it.hasNext(); ){
System.out.print(it.next().toString());
}
System.out.println("");
System.out.println("Not Allowed in Identifiers:");
for (Iterator it = other.iterator(); it.hasNext(); ){
System.out.print(it.next().toString());
}
}
}