About the Java Escape
Java accepts a shorter list of escape sequences than most languages: \b, \t, \n, \f, \r, the two quote characters, the backslash itself, octal escapes, and \uXXXX. There is no \v and no \x form, which is where hand-written escaping usually goes wrong. This converter respects that list, so a vertical tab or a bell character is emitted as \u000B or \u0007 rather than a sequence javac would reject.
Escaping the single quote is optional inside a string but required inside a char literal, so the dropdown lets you cover both. Ticking the non-ASCII option rewrites every character above the ASCII range as a four-digit Unicode escape, which matters because javac reads source using the platform encoding unless you pass -encoding UTF-8. An all-ASCII literal compiles identically on every machine.
One quirk worth knowing: Java resolves \uXXXX during lexing, before it decides what is a string, so a stray Unicode escape in a comment can still break a build. Escaping the whole value here avoids that. Use Java Unescape for the reverse trip, or JSON to Java when you want classes rather than a literal.
How to use
- Paste the value you want to hard-code into Java.
- Choose whether single quotes should be escaped as well, which you need for a char literal.
- Tick the non-ASCII option if the source file must stay pure ASCII.
- Copy the result between the quotes of your literal.
Common questions
- Why is a vertical tab written as \u000B?
- Java has no \v escape. The Unicode form is the only way to put that character in a literal without pasting the raw byte.
- Do I need to escape non-ASCII characters?
- Not if your build sets the source encoding to UTF-8. If it does not, escaping them removes any dependence on the platform default.
- Is the output valid inside a text block?
- Yes, though a text block already allows raw newlines and unescaped double quotes, so escaping there is optional.
- Does it escape a dollar sign?
- No. The dollar sign is an ordinary character in Java strings; it only carries meaning in templating libraries.