Why am I not allowed to use the š„ž symbol in the Java source file as a variable name?

What are the rules for characters that can be used in Java variable names?

I have this code example:

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 does the Java compiler throw an error for 'š„ž'? (\ UD834 \ uDD1E)

Same thing at ideone.com: http://ideone.com/fnmvpG

+5
source share
1 answer

What are the rules for characters that can be used in Java variable names?

Rules are specified in the JLS for identifiers, section 3.8 . You are interested in Character.isJavaIdentifierPart :

A character can be part of a Java identifier if one of the following conditions is true:

  • this letter
  • it is a currency symbol (e.g. $$)
  • it is a connecting punctuation character (for example, "_")
  • this is a figure
  • it is a numeric letter (e.g. Roman numeral character)
  • it is a combining sign
  • it is a sign without a space
  • isIdentifierIgnorable (codePoint) returns true for the character

Of course, it is assumed that you compile your code with the appropriate encoding.

The character you are apparently trying to use is U + 1D11E , which is none of the above. This is a musical treble clef, which is located in the "Symbols, Others" category.

+6
source

Source: https://habr.com/ru/post/1241749/


All Articles