Why am I not allowed to use the character 𝄞 in a Java source code file as a variable name?

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

Jon Skeet
people
quotationmark

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:

  • it is a letter
  • it is a currency symbol (such as '$')
  • it is a connecting punctuation character (such as '_')
  • it is a digit
  • it is a numeric letter (such as a Roman numeral character)
  • it is a combining mark
  • it is a non-spacing mark
  • isIdentifierIgnorable(codePoint) returns true for the character

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.

people

See more on this question at Stackoverflow