2

I'm trying to initialize a string from hex:

import java.io.*;

public class a {
    public static void main (String[] args)
    {
        try {
            PrintWriter w = new PrintWriter("something"); 
            w.println("\u0061\u0062\u000a");
            w.close(); 
        } catch (Exception e) {};
    }
}

But looks like "\u000a" is a badchar, it kept telling me unclosed string literal?

a.java:8: error: unclosed string literal
            w.println("\u0061\u0062\u000a");

Any idea what's wrong with it?

Running:

%> java -version
java version "1.7.0_45"
Java(TM) SE Runtime Environment (build 1.7.0_45-b18)
Java HotSpot(TM) 64-Bit Server VM (build 24.45-b08, mixed mode)
daisy
  • 22,498
  • 29
  • 129
  • 265

1 Answers1

6

That's a consequence of how \u is handled in Java. These unicode escapes are processed very early in the compilation, even before the tokenizer recognizes the string literal.

Therefore the code snippet is equivalent to:

w.println("ab
");

Which is clearly not correct.

To fix that use \n instead of \u000a.

Henry
  • 42,982
  • 7
  • 68
  • 84