Java String Encoder
Escape special characters for safe use in Java string literals, or decode Java escape sequences back to plain text
Paste raw text, click Encode, and get a Java-safe string with quotes, backslashes, control characters, and Unicode all properly escaped. Decoding works the other way — drop in an escaped Java literal and read the plain text behind it. And everything runs in your browser. Nothing gets uploaded.
How It Works
The tool scans each character in your input. Anything that would break a Java string literal — quotes, backslashes, newlines, tabs, null bytes — gets replaced with its escape sequence. Toggle Unicode mode and non-ASCII characters become \uXXXX sequences. Characters above U+FFFF (like emoji) get split into surrogate pairs automatically, which is exactly how Java's UTF-16 encoding expects them.
Examples
Escaping a JSON snippet for a Java constant
You need to embed a JSON template as a string constant in your Java class.
{"name": "O'Brien", "path": "C:\Users"}
{\"name\": \"O'Brien\", \"path\": \"C:\\Users\"}
Multi-line SQL query with Unicode
A query that includes accented names and line breaks — common in internationalized apps.
SELECT *
FROM users
WHERE name = 'René'
SELECT *\nFROM users\nWHERE name = 'Ren\u00e9'
Emoji and supplementary characters
User-facing strings with emoji need surrogate pairs in Java's UTF-16 encoding.
Hello 🚀 World
Hello \ud83d\ude80 World
Who Uses This
Embedding SQL templates, file paths, or config snippets inside Java string constants.
Preparing user-facing strings with special characters and emoji for resource files.
Learning how Java escape sequences work by comparing raw text to its encoded form.
Building test data with edge-case characters — nulls, tabs, Unicode boundaries — for string-handling tests.
Common Mistakes
You encode a file path, then paste the result into code that adds its own escaping layer. Now C:\Users becomes C:\\\\Users. Only encode once — right before it goes into the final Java literal.
Accented characters like é or ü look fine in your IDE, but older build pipelines with ASCII-only encoding will corrupt them. Turn on Unicode escape if your toolchain doesn't guarantee UTF-8 everywhere.
Text blocks ("""...""") only work in Java 13+ (preview) and Java 15+ (stable). If your project targets an older JDK, stick with regular string literals and standard escape sequences.
Technical Details
- Processing: 100% client-side — nothing leaves your browser
- Browser Support: Chrome, Firefox, Safari, Edge
- Escape Coverage: Quotes, backslash, newline, tab, CR, backspace, form feed, null, Unicode (\uXXXX), octal (\OOO)
- Text Block Mode: Java 13+ triple-quote format with proper indentation handling
- Supplementary Characters: Full surrogate pair encoding for characters above U+FFFF
Frequently Asked Questions
What characters need to be escaped in a Java string?
Double quotes, backslashes, and control characters — that's the short list. Specifically: ", \, \n, \t, \r, \b, \f, and \0. If you're working with non-ASCII text and want to be safe across all build environments, you can also escape those to \uXXXX format.
How do I encode emoji in a Java string?
Emoji sit above U+FFFF in Unicode, and Java uses UTF-16 internally. That means each emoji becomes a surrogate pair — two \uXXXX escapes back to back. This tool handles that conversion automatically. Just paste the emoji, enable Unicode escape, and you get the correct pair.
What's the difference between a Java text block and a regular string?
Regular strings use one pair of double quotes and need explicit \n for line breaks. Text blocks use triple quotes (""") and let you write multi-line content directly. They were added in Java 13 as a preview feature and became stable in Java 15. The escaping rules differ slightly — you don't need to escape single quotes inside a text block, for instance.
Can I decode a Java-escaped string back to plain text?
Yes. Paste the escaped string into the input box and click Decode. It reverses all standard Java escape sequences including \uXXXX Unicode escapes, \OOO octal escapes, and surrogate pairs. Handy when you're reading someone else's code and can't tell what a string actually contains.
Is my data safe when using this tool?
Completely. The encoding and decoding happen in JavaScript right inside your browser tab. No server requests, no data storage, no analytics on your input. Disconnect from the internet and it still works. Your text stays on your device the entire time.