0

When i use new line character in comment like this //System.out.print("\n Hi"); no problem in compilation. But when i try to use Unicode value instead of special character for new line like this //System.out.print("\u000d HI"); it omits Compilation Error? Does unicode value not support in comment, if no then Why? Please Explain

Mathur
  • 71
  • 7
  • 1
    Your question is unclear. If it's in a comment, what is the meaning of "it is working"? How is it "not working" when a comment is ignored by the compiler? What are you expecting should happen? – Jim Garrison Jan 27 '17 at 06:42
  • Interesting `System.out.print(\u000d "HI");` compiles and works but `//System.out.print(\u000d "HI");` has a compile error as the `\u000d` seems to be wrong interpretted – Scary Wombat Jan 27 '17 at 06:53
  • 1
    Unicode sequences (such as `\u000d`) are processed _very early_ by the compiler. When you use one, it is replaced with the equivalent character before the compiler starts parsing anything. To see what I mean, try putting `System.out.println(\u0022Hello");` in a program (0x0022 is the code for a double quote mark). – ajb Jan 27 '17 at 06:55
  • @ScaryWombat Shouldn't be surprising... both cases work exactly as if you had a newline in your code instead of `\u000d`. In the first example, it's like you had `System.out.print("HI");` but put the parameter on a new line, which is fine in Java. In the second case, the newline terminates the comment, so it's like you had `"HI");` all by itself on a line, which is bad syntax. – ajb Jan 27 '17 at 06:59
  • By the way, "omit" means to leave out, so you said the compiler leaves out the Compilation Error. I think you mean "emit". I don't normally harp on misspellings, but occasionally a misspelling can reverse the entire meaning of a sentence. – ajb Jan 27 '17 at 07:01
  • @ajb Yep **not** interesting when I think about it. – Scary Wombat Jan 27 '17 at 07:01
  • @ajb thanks for explanation, you mean first compiler put unicode value then go for comment like this //System.out.print("; Hi"); – Mathur Jan 27 '17 at 07:06
  • 1
    Pretty much. The compiler first changes `\u000d` to a newline. Then it parses the comment, and since a comment ends at the end of the line, the newline that it put in earlier, when it saw your `\u000d`, will end the comment. Basically, it's a bad idea to use `\uxxxx` for any character in the range 0000 to 007f, because it's not useful and can have surprising results. – ajb Jan 27 '17 at 07:10
  • @ajb Thanks again – Mathur Jan 27 '17 at 07:12

0 Answers0