In Java, the meaning of s depends on context. It can be an ordinary variable name, the general formatting conversion %s, the string escape \s, or part of a regular expression. Java source escaping and regex syntax are separate layers: "\s" and "\\s" do different things.
Contents
A quick comparison
| Java source | Meaning | Example result |
|---|---|---|
String s = "hello"; | An ordinary identifier chosen by the programmer. | s refers to that string variable. |
String.format("%s", 42) | Formatter’s general string conversion. | "42" |
"\s" | In Java 15+, one space character, U+0020. | A string of length 1. |
"\\s" | A backslash and s in the resulting string; the regex engine interprets them as a whitespace class. | Matches regex whitespace when used as a pattern. |
"(?s).+" | Regex DOTALL mode: dot also matches line terminators. | Can match text spanning a newline. |
The important distinction: string escape versus regex
The language specification’s escape-sequence section, §3.10.7 includes \s for a space. It became a standard escape with Java 15. Older source levels may reject it. It is valid in an ordinary string literal on supported versions, not only inside text blocks.
String space = "\s"; // one literal space, Java 15+
String regex = "\\s+"; // regex text: backslash, s, plus
String[] words = "a\tb c".split(regex); // [a, b, c]
If you accidentally write split("\s+") on a modern Java version, the code can compile while meaning “one or more ordinary spaces,” because Java has already converted \s to a space. It will not split the tab in the same way. This is a semantic bug, not necessarily a compiler error.
Regex whitespace and Unicode
Oracle’s Pattern reference defines default regex \s as the ASCII whitespace class consisting of space, tab, newline, vertical tab, form feed and carriage return. Unicode behavior changes with UNICODE_CHARACTER_CLASS or embedded (?U). For example:
boolean ascii = "\u00A0".matches("\\s"); // false
boolean unicode = "\u00A0".matches("(?U)\\s"); // true
Here \u00A0 denotes a non-breaking space. Choose the class your input format requires rather than assuming every visually blank character is handled identically. Character.isWhitespace, String.strip and regex classes should not be assumed to have exactly the same character set.
(?s) is DOTALL; (?m) is MULTILINE
DOTALL changes what dot matches. MULTILINE changes the behavior of anchors such as ^ and $ around line boundaries. These are different operations:
boolean a = "a\nb".matches("a.b"); // false
boolean b = "a\nb".matches("(?s)a.b"); // true
boolean c = "a\nb".matches("(?m)a.b"); // false
Do not insert spaces into the flag expression as ( ?s ). Also distinguish matches(), which attempts a whole-input match, from Matcher.find(), which searches for a matching subsequence.
%s formats more than String objects
In Formatter, %s is a general conversion. For ordinary non-null objects it normally uses their string representation; objects implementing Formattable have special formatting behavior. A null argument is formatted as "null".
System.out.printf("Name: %s; count: %s%n", "Ada", 3);
// Name: Ada; count: 3
System.out.println(String.format("%s", (Object) null));
// null
Run the complete checked example
Download JavaSExamples.java. Compile with a JDK supporting the chosen source level, then enable assertions:
javac --release 17 JavaSExamples.java
java -ea JavaSExamples
The supplied standalone example checks the string escape, regex whitespace, Unicode flag, DOTALL behavior and formatting. It does not require a library or network connection. This revision’s examples were compiled and executed with assertions enabled; the historical saved answer remains available through the previous-version link.
Common questions
Is s a Java keyword?
No. A variable named s has the type and value declared by the surrounding code.
Why do regex examples use two backslashes?
One source-level escape produces a literal backslash in the string so the regex parser can read the resulting pattern.
Which version should I check?
Both the runtime JDK and the compiler’s source or release target matter. A newer installed JDK can still compile for an older language level.
Related learning
Use the mathematics roadmap for logic and discrete mathematics, and font-selection guidance when preparing code examples in documents.
Article history
Sources checked October 22, 2026
- Corrected Java15+ literal \s to one U+0020 space, versus Java source \\s for a regex.
- Distinguished default regex whitespace, Unicode character class, DOTALL and MULTILINE.
- Compiled and ran downloadable Java17 examples with assertions; %s supports ordinary objects and null.
