What are the rules for the characters that can be used in Java variable names?
I have this sample code:
public class Main {
public static void main(String[] args) {
int kš = 4;
System.out.println(s);
}
}
which will not compile:
javac Main.java
Main.java:3: error: illegal character: '\udd1e'
int kš = 4;
^
1 error
So why is the Java compiler throwing an error for 'š' ? (\uD834\uDD1E)
Same in ideone.com: http://ideone.com/fnmvpG
What are the rules for the characters that can be used in Java variable names?
The rules are in the JLS for identifiers, in section 3.8. You're interested in Character.isJavaIdentifierPart
:
A character may be part of a Java identifier if any of the following are true:
Of course, that assumes you're compiling your code with the appropriate encoding.
The character you're apparently trying to use is U+1D11E, which is none of the above. It's a musical treble clef, which is in the "Symbols, other" category.
See more on this question at Stackoverflow