Why is java compiler not complaining about "\s"?

287 Views Asked by At

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.

2

There are 2 best solutions below

4
Lev M. On BEST ANSWER

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.

0
Arvind Kumar Avinash On

\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.