2

Tried it on openjdk version "15" 2020-09-15

Following simple line

String str = "\s";

According to official documentation of list of escape characters, \s is not defined.

Spring tool suite does not show compilation error for '\s' nor does when I compile on command line using maven. Any other characters such as \p result into following error which matches with official documentation.

Invalid escape sequence (valid ones are \b \t \n \f \r " ' \ )

Why doesn't java compiler complain about \s? Printing the resultant string on console indicates that it's outputting space character (0x20), so looks like it's recognizing it, but just not documented.

Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
user871199
  • 1,420
  • 19
  • 28

2 Answers2

6

This is a new escape sequence in Java 15:
https://docs.oracle.com/en/java/javase/15/text-blocks/index.html

The tutorial you linked to is for Java 8, but you are on JDK 15.

Lev M.
  • 6,088
  • 1
  • 10
  • 23
  • Shouldn't error message mention it? It still says valid escapes from older list – user871199 Sep 14 '21 at 21:09
  • 3
    @user871199 [The language specification under 3.10.7. Escape Sequences](https://docs.oracle.com/javase/specs/jls/se15/html/jls-3.html#jls-3.10.7) mentions "s" as a valid escape character, so there will be no error message. – Scratte Sep 14 '21 at 21:15
  • @Scratte, but error message is very explicit - exactly as in the post. Minor issue, but still issue. If error message is giving a list, then one tends to assume it's a complete list. – user871199 Sep 14 '21 at 21:19
  • 3
    @user871199 I expect the explanation is that a developer forgot to update the error message. You could try to contact OpenJDK and see if you can submit a bug-report on the message. – Scratte Sep 14 '21 at 21:22
1

\s is a valid escape sequence in Java SE 15 as specified in the String#translateEscapes documentation:

enter image description here

The error message that you have mentioned is definitely a bug.

Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110