Java Escape / Unescape
Escape and unescape Java strings — convert special characters like \n, \t, \r, \", \\ to Java escape sequences and back.
Input
Output
What Is Java String Escaping?
In Java, string literals must have certain characters escaped so the compiler can parse them correctly. Backslashes, double quotes, single quotes, newlines, tabs, carriage returns, backspaces, and form feeds all need to be represented as escape sequences. For example, a literal newline inside a Java string must be written as \n, and a backslash must be written as \\. This follows the rules defined in the Java Language Specification — String Literals.
This tool works in two modes: Escape turns raw text into a safe Java string literal, and Unescape converts an escaped Java string back to human-readable text. Everything runs locally in your browser — nothing is sent to a server. For the full list of Java escape sequences, see Oracle's Java Character tutorial.
How to Use This Tool
Choose Mode
Select Escape to convert raw text to a safe Java string, or Unescape to convert an escaped Java string back to plain text.
Paste Your Text
Paste your input into the left editor. You can also click Sample to load a realistic example, or Upload to load a file.
Get the Result
The right panel updates automatically as you type. Use Copy or Download to get the result. To format Java code, try Java Formatter.
Java Escape Examples
Here is a real-world example. The raw text contains a newline, a tab, a backslash, and double quotes:
Raw input (with newline, tab, backslash, and quotes)
Escaped Java string output
When Java String Escaping Matters
Any time you embed text in a Java string literal you need to escape special characters. This comes up when building SQL queries, constructing JSON strings manually, writing log messages with file paths, or embedding HTML inside a Java string. Forgetting to escape a backslash in a Windows file path (C:\Users\name) is one of the most common Java beginner bugs. The java.lang.String class documentation covers how strings are stored in the JVM after parsing.
Java 15+ introduced text blocks ("""...""") which reduce the need for manual escaping in multiline strings. See the JEP 378 Text Blocks spec for details. For JSON-specific escaping, try the JSON Escape tool.
Frequently Asked Questions
What characters need to be escaped in a Java string?
The characters that must be escaped in Java string literals are: backslash (\\), double quote (\"), single quote (\'), newline (\n), carriage return (\r), tab (\t), backspace (\b), and form feed (\f). Non-ASCII characters can also be represented as Unicode escapes like \u00E9.
What is the difference between \n and an actual newline in Java?
In a Java string literal, writing an actual newline character (pressing Enter) causes a compile error. You must use \n instead, which the compiler translates to the Unicode character U+000A (line feed). Java 15+ text blocks are the exception — they do allow real newlines inside the """...""" delimiters.
How do I escape a backslash in Java?
A single backslash in Java source code must be written as two backslashes: \\. So a Windows path like C:\Users\name must be written as "C:\\Users\\name" in a Java string literal. This is the most common escaping mistake for developers coming from other languages.
What are Java Unicode escape sequences?
Java supports Unicode escapes in the form \uXXXX where XXXX is a four-digit hexadecimal number representing the Unicode code point. For example, \u00E9 is é and \u4E2D is 中. These can appear anywhere in Java source code, not just in string literals. See the JLS Unicode Escapes section for the full rules.
Does this tool send my data to a server?
No. All escaping and unescaping happens entirely in your browser using JavaScript. Your input is never transmitted anywhere. This makes the tool safe for escaping sensitive strings like passwords, API keys, or private data.
Related Tools
References: Java Language Specification — String Literals · Oracle Java Characters Tutorial · JEP 378 Text Blocks