What does “\1” represent in this Java string?

前端 未结 2 437
小蘑菇
小蘑菇 2021-01-03 19:43
System.out.println(\"\\1\");

I thought it did not compile because of the non-recognized escape sequence.

What does \"\\1\" exa

2条回答
  •  猫巷女王i
    2021-01-03 20:16

    It's an octal escape sequence, as listed in section 3.10.6 of the JLS. So for example:

    String x = "\16";
    

    is equivalent to:

    String x = "\u000E";
    

    (As Octal 16 = Hex E.)

    So \1 us U+0001, the "start of heading" character.

    Octal escape sequences are very rarely used in Java in my experience, and I'd personally avoid them where possible. When I want to specify a character using a numeric escape sequence, I always use \uxxxx.

提交回复
热议问题