Naming restrictions of variables in java
java
Solution
This is not the case - many special characters are actually valid for identifiers. It is defined in the JLS #3.8:
An identifier is an unlimited-length sequence of Java letters and Java digits, the first of which must be a Java letter. [...] 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.
For example, this is a valid variable name:
String sçèêûá¢é£¥ = "bc";
You can see all the valid characters with this simple code:
public static void main(String args[]) {
for (int i = 0; i < Character.MAX_VALUE; i++) {
if (Character.isJavaIdentifierPart(i)) {
System.out.println("i = " + i + ": " + (char) i);
}
}
}
ps: nice examples on @PeterLawrey's blog
Problem
Why are special characters (except `$`, `_`) not allowed in Java variable names?