System.out.println("\1");
I thought it did not compile because of the non-recognized escape sequence.
What does "\1"
exactly represent?
System.out.println("\1");
I thought it did not compile because of the non-recognized escape sequence.
What does "\1"
exactly represent?
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
.
// This \n is okay
is fine, but // This \u000a is not
will not compile. –
Mazer \\1
, which can be quite irritating. –
Dominations \u
. This is because Java will interpret \u
as starting a a hex escape sequence anywhere in your Java code, including in comments and identifiers. The compiler replaces the character before compiling your code, potentially leading to strange bugs. For example: github.com/antlr/antlr4/issues/164 –
Antiphon \uxxxx
in a string like a file path (like the bug I posted), not deliberately using it to specify a special character. –
Antiphon © 2022 - 2024 — McMap. All rights reserved.