57

I stumbled over this (again) today:

class Test {
    char ok = '\n';
    char okAsWell = '\u000B';
    char error = '\u000A';
}

It does not compile:

Invalid character constant in line 4.

The compiler seems to insist that I write '\n' instead. I see no reason for this, yet it's very annoying.

Is there a logical explanation why characters that have a special notation (like \t, \n, \r) must be expressed in that form in Java source?

1
  • 1
    You can replace all the source in your code with \uXXXX sequences to make it unreadable, but it will compile fine as it converts all these to the text before compiling. Mar 7, 2013 at 18:09

5 Answers 5

85

Unicode characters are replaced by their value, so your line is replaced by the compiler with:

char error = '
';

which is not a valid Java statement.

This is dictated by the Language Specification:

A compiler for the Java programming language ("Java compiler") first recognizes Unicode escapes in its input, translating the ASCII characters \u followed by four hexadecimal digits to the UTF-16 code unit (§3.1) of the indicated hexadecimal value, and passing all other characters unchanged. Representing supplementary characters requires two consecutive Unicode escapes. This translation step results in a sequence of Unicode input characters.

This can lead to surprising stuff, for example, this is a valid Java program (it contains hidden unicode characters) - courtesy of Peter Lawrey:

public static void main(String[] args) {
    for (char c‮h = 0; c‮h < Character.MAX_VALUE; c‮h++) {
        if (Character.isJavaIdentifierPart(c‮h) && !Character.isJavaIdentifierStart(c‮h)) {
            System.out.printf("%04x <%s>%n", (int) c‮h, "" + c‮h);
        }
    }
}
3
  • 16
    +1 Good to know. So if I use System.out.print("Hello ");//\u000ASystem.out.println("World"); it prints "Hello World" even if second print is teoreticly in comment:D
    – Pshemo
    Mar 7, 2013 at 16:20
  • 7
    You were first, I believe, so I accepted this one :) Although that perfectly explains the cause behind it, I'm still curious why the language creators thought that early expansion was a good idea. It still strikes me as a big WTF.
    – Durandal
    Mar 7, 2013 at 16:24
  • 1
    @Durandal Doing that enables you to use characters that can't be represented in the charset of the source file (not that it has ever happen to me!).
    – assylias
    Mar 8, 2013 at 12:18
23

Unicode escape sequences like \u000a are replaced by the actual characters they represent before the Java compiler does anything else with the source code. And so, your program eventually ends up at

char ch = '
';

So the \u000a in your source code is replaced internally by a linefeed character. Note that this happens before the compiler actually reads and interprets your source code.

Referring to the Java Language Specification:

It is a compile-time error for a line terminator (§3.4) to appear after the opening ' and before the closing '.

And as well all know by heart, \n is a line terminator, quoting:

 LineTerminator:
    the ASCII LF character, also known as "newline"
    the ASCII CR character, also known as "return"
    the ASCII CR character followed by the ASCII LF character

Other symbols that could cause problems are \, ' and " for example.

2
  • 1
    The program looks like what?
    – wallyk
    Mar 7, 2013 at 16:16
  • There seem to be a lot more than 3: \u0027 (') \u005c (\) also create havoc. \u0034 (") breaks String literals.
    – Durandal
    Mar 7, 2013 at 16:47
4

I think the reason is that \uXXXX sequences are expanded when the code is being parsed, see JLS §3.2. Lexical Translations.

0
4

It is described in 3.3. Unicode Escapes http://docs.oracle.com/javase/specs/jls/se7/html/jls-3.html. Javac first finds \uxxxx sequences in .java and replaces them with real characters then compiles. In case of

char error = '\u000A';

\u000A will be replace with newline character code (10) and the actual text will be

char error = '
';
2

Because the compiler treats them the same as unescaped text.

This is valid code:

 class \u00C9 {}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.