Quick revision
The short answer
Java translates Unicode escapes, a backslash and u followed by four hex digits, before the compiler even splits the source into tokens, so they are processed everywhere, including inside comments and strings. In // see C:\users\dev, the \u starts an escape but is followed by s, which is not a hex digit, so the compiler reports an illegal Unicode escape. And \u000a is the escape for a line feed, so it becomes a real line break, which ends the comment and turns the text after it into code. We avoid this by writing UTF-8 source with normal characters, and by having review tools flag Unicode escapes and bidirectional control characters.
What to remember
In this answer, we saw that the compiler translates Unicode escapes before it finds comments, strings or tokens. That is why \users in a comment is an illegal escape that stops compilation, and why \u000a becomes a real line break that ends a comment and turns the rest of the line into code. We avoid both by writing UTF-8 source with real characters and an explicit build encoding, and we protect reviews by flagging Unicode escapes and bidirectional control characters, the trick behind the Trojan Source attacks.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
Java translates Unicode escapes, a backslash and u followed by four hex digits, before the compiler even splits the source into tokens, so they are processed everywhere, including inside comments and strings. In // see C:\users\dev, the \u starts an escape but is followed by s, which is not a hex digit, so the compiler reports an illegal Unicode escape. And \u000a is the escape for a line feed, so it becomes a real line break, which ends the comment and turns the text after it into code. We avoid this by writing UTF-8 source with normal characters, and by having review tools flag Unicode escapes and bidirectional control characters.
Why this question is asked
This question looks like trivia, but it describes real problems: a Windows path in a comment that breaks the build, a code change that looks commented out but runs, and the 2021 Trojan Source attacks, where code shown in a review was not the code that compiled. Interviewers ask it because the explanation requires knowing the order in which the compiler reads source text, which most developers have never had to think about. In this answer, we will recap what a Unicode escape is, see where it fits in the compiler's reading of a file, explain both failures from the question, and then look at how to write and review source code so neither can happen.
1A Quick Recap of Unicode Escapes
Java source code is written in Unicode, but in the early days many editors and file systems could only handle ASCII. So the language allowed any character to be written as a Unicode escape: a backslash, the letter u, and four hexadecimal digits giving the character's code. \u0041 is the letter A, \u00e9 is e with an acute accent, and \u000a is a line feed, the character that ends a line.
The important rule is where these escapes are handled. The Java Language Specification describes reading a source file in steps, and translating Unicode escapes is the very first step, before the compiler finds line endings, comments, strings or tokens. So at that moment the compiler does not know or care whether a backslash is inside a comment, a string or code. Every \u it sees must be a valid escape, and it is replaced by its character before anything else happens.
- Read the characters of the fileusing the source encoding, such as UTF-8
- Translate Unicode escapesevery \u plus four hex digits becomes its character
- Find line terminatorsa translated \u000a now ends a line
- Split into comments, whitespace and tokensa // comment ends at the line end
- Parse and compile the tokens
5 steps in order. Play them, or step through them one at a time.
Escape translation happens before the compiler knows what is a comment, so comments get no protection.
Notice the third line. Because escapes are translated first, even an identifier can be written with them. That is legal, and it is also a sign of how early and how blindly this step runs.
2Failure 1: A Windows Path in a Comment
Now the first failure is easy to explain. In the comment // see C:\users\dev, the compiler meets a backslash followed by u while translating escapes. It expects four hex digits next, but finds s, e, r, s. That is not a valid escape, and since this step runs before comments are recognised, the compiler cannot ignore it. It stops with an error saying the Unicode escape is illegal.
Two details matter. First, only a lowercase u starts an escape, so C:\Users\dev with a capital U happens to compile, which is why this bug often appears only on some machines or in some paths. Second, a backslash only starts an escape if it is preceded by an even number of backslashes, including zero. In C:\\users the second backslash is preceded by one backslash, so it does not start an escape, and the comment compiles.
3Failure 2: \u000a Turns a Comment Into Code
The second failure is more dangerous because it compiles. \u000a is a valid escape for the line feed character. It is translated in the first step, so by the time the compiler looks for line endings, the comment really contains a line break. A // comment ends at the line break, so everything after it is on a new line and is compiled as code.
Notice how innocent the original looks. A reviewer reads a comment, but the program sets admin to true. The same translation explains a smaller surprise: char c = '\u000a'; does not compile, because the escape becomes a real line break inside the character literal, which is not allowed. We must write the ordinary escape sequence '\n' instead, because escape sequences like \n are handled much later, inside the string or character literal, and never create a line break in the source.
4How to Write Source That Avoids Both
The best practice is simple: save source files as UTF-8 and type the real characters. A string written with the actual accented letter is clearer than a \u00e9 escape, and every modern editor and build tool handles it. To make that reliable, we set the encoding explicitly in the build, for example with javac -encoding UTF-8 or the project encoding setting in Maven or Gradle, so every machine reads the files the same way.
We keep Unicode escapes only for rare cases where a character would be invisible or confusing on screen, such as a non-breaking space or a zero-width character in a test string, and then we put a comment next to them saying which character they are. In comments, we write paths with forward slashes or doubled backslashes.
5Reviewing for Hidden Characters: Trojan Source
In 2021, researchers published the Trojan Source attacks, tracked as CVE-2021-42574. They used Unicode bidirectional control characters, the invisible characters that tell a display to show text right to left, inside comments and strings. An editor or code review page then shows the code in a different order from the order the compiler reads it, so a reviewer can approve code that does something else. Unicode escapes like \u000a have the same effect of making the displayed code differ from the compiled code.
So our review tools should flag both: any \u escape in source code, and any bidirectional control character in the file. Many code hosts now warn about bidirectional characters in diffs, and a simple check in the build that fails on them, with a short allow-list for the rare intended escapes, closes the gap for the rest.
6Follow-up questions
Are escape sequences like \n and \t affected in the same way?
No. \n, \t and the other escape sequences are only interpreted inside string and character literals, after tokens have been found. They never produce a real line break in the source, which is why '\n' compiles and '\u000a' does not.
Why did Java design escapes to run before everything else?
So that a file written only in ASCII could express any Unicode program, including identifiers and keywords, in the 1990s when tools could not handle Unicode. Translating escapes first was the simplest way to make the rest of the compiler work only on Unicode characters. The cost is the surprising behaviour in comments that we saw here.
Does a text block change any of this?
No. Text blocks, added in Java 15, are still read after escape translation, so a \u escape inside one is translated first like everywhere else. Text blocks remove the need for many \n sequences, but not the rules for Unicode escapes.
7Conclusion
In this answer, we saw that the compiler translates Unicode escapes before it finds comments, strings or tokens. That is why \users in a comment is an illegal escape that stops compilation, and why \u000a becomes a real line break that ends a comment and turns the rest of the line into code. We avoid both by writing UTF-8 source with real characters and an explicit build encoding, and we protect reviews by flagging Unicode escapes and bidirectional control characters, the trick behind the Trojan Source attacks.
Open with
Both come from the same rule: Java translates Unicode escapes, a backslash, a u and four hex digits, as the very first step of reading the source, before it knows what is a comment, a string or code.
Then, if they want more
In the first case, the comment contains C:\users, so the compiler sees a backslash followed by u and expects four hex digits, but finds s, e, r, s. That is an illegal Unicode escape, and because it is translated before comments are recognised, the comment does not protect it and compilation fails.
A capital U, forward slashes or doubled backslashes would avoid it. In the second case, \u000a is a valid escape for a line feed. It is turned into a real line break, the single-line comment ends there, and whatever follows on that line is compiled as code, so something that looks commented out actually runs.
For the same reason a char literal containing \u000a does not compile, and I would write backslash n instead.
In practice I would keep source files in UTF-8 with the encoding set in the build, write real characters rather than escapes, and have the review tooling flag Unicode escapes and bidirectional control characters, which is how the Trojan Source attacks hid code from reviewers.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 15
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- The Java Language Specification, Java SE 21, Java SE 21 API documentation
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "This code fails to compile even though the problem line is inside a comment: // see...": https://scrivio-interview-library.netlify.app/java/operators-and-control-flow/32d25a98eb/